2
How can I select the first line of each conversation from a specific user where the to_id = 1
.
The big problem is when the first message exchanged from the conversation does not have the top_id = 1
and ends up listing the next message of the conversation that has the from_id = 1
, when you shouldn’t have.
Here is the link to the SQLFIDDLE: http://www.sqlfiddle.com/#! 9/7a772b/4
In SQLFIDDLE you are listing the Test 1
and the Test 4
when you should be listing only the Test 1
, why the conversation Test 4
begins in the Test 3
where there is no to_id = 1
.
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
Dude, when a user sends two messages before the other user sends at least one, they end up not listing. Do you have any idea how to fix this?
– Diego Vieira
http://www.sqlfiddle.com/#! 9/8c8e09/2 In this example you should continue listing "test 1" and "test 3", but list only "test 1"
– Diego Vieira
I saw your edition and is returning the test 3 straight, but the problem is that is returning the test 4 also.
– Diego Vieira
I added the IFNULL clause for when there is no reply and the OR after WHERE to filter when the user sends two or more messages in a row.
– Marcel
Show! Thank you very much!
– Diego Vieira