How can I select the first line of each conversation for a specific user where to_id = 1
.
The big problem is when the first message changed from the conversation does not have top_id = 1
and ends up listing the next message in the conversation that has from_id = 1
, when it should not.
Here is the link to SQLFIDDLE: link
In SQLFIDDLE you are listing Test 1
and Test 4
when you should be listing only Test 1
, why Test 4
conversation starts in Test 3
where to_id = 1
does not.
SQL
SELECT t1.*, m2.message, m2.from_id FROM
(SELECT to_id,message, MIN(created_at) AS created_at FROM messages m
WHERE to_id = 1
GROUP BY to_id,message) AS t1
INNER JOIN messages m2 ON t1.created_at = m2.created_at