I have big problem with mysql because Im frontend developer and Im very poor with SQL ^^
I have 3 tables in database + user table
participants:
+----+---------+-----------------+---------------------+
| id | user_id | conversation_id | updated_at |
+----+---------+-----------------+---------------------+
| 1 | 1 | 1 | 2022-01-03 00:00:00 |
| 2 | 2 | 1 | 2022-01-03 00:00:00 |
| 3 | 1 | 2 | 2022-01-03 00:00:00 |
| 4 | 3 | 2 | 2022-01-03 00:00:00 |
| 5 | 1 | 2 | 2022-01-03 00:00:00 |
+----+---------+-----------------+---------------------+
conversations:
+----+---------------------+
| id | updated_at |
+----+---------------------+
| 1 | 2022-01-03 00:00:00 |
| 2 | 2022-01-03 00:00:00 |
+----+---------------------+
messages:
+----+----------------+-----------------+-------------------+----------------------+
| id | participant_id | conversation_id | message | updated_at |
+----+----------------+-----------------+-------------------+----------------------+
| 1 | 1 | 1 | test msg 1 room 1 | 2022-01-03 00:00:00 |
| 1 | 1 | 1 | test msg 2 room 1 | 2022-01-05 00:00:00 |
| 1 | 2 | 1 | test msg 3 room 1 | 2022-01-07 00:00:00 |
| 1 | 3 | 2 | test msg 1 room 2 | 2022-01-03 00:00:00 |
| 1 | 2 | 2 | test msg 2 room 2 | 2022-01-04 00:00:00 |
| 1 | 1 | 2 | test msg 3 room 2 | 2022-01-05 00:00:00 |
| 1 | 1 | 1 | test msg 4 room 1 | 2022-01-14 00:00:00 |
+----+----------------+-----------------+-------------------+----------------------+
So I'm trying to get last message from each rooms but I have problem, when I'm trying to group by then I have error like this
Error Code: 1055. Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'zzzz' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
Its possible to get last messages by each room? like this
+-----------------+-------------------+----------------------+
| conversation_id | message | updated_at |
+-----------------+-------------------+----------------------+
| 2 | test msg 3 room 2 | 2022-01-05 00:00:00 |
| 1 | test msg 4 room 1 | 2022-01-14 00:00:00 |
I tried something like this, but not working...
WITH RECURSIVE test AS (
SELECT
pp.user_id,
pp.conversation_id,
pp.updated_at
FROM
participants pp
UNION
SELECT
p.user_id,
p.conversation_id,
p.updated_at
FROM
participants p
INNER JOIN conversations c ON c.id = p.conversation_id
) SELECT
m.conversation_id,
m.message,
m.updated_at
FROM
test t
INNER JOIN messages as m ON m.conversation_id = t.conversation_id
ORDER BY m.updated_at DESC
;