I have the following table. I want to get the last message of each conversation. Please help with a proper query to get it.
ID ID2 User1 User2 Message TimeStamp
---- ----- ------- ------- ------------------------- ------------------------
1 1 1 2 hii March, 04 2017 8:30:00
1 2 2 1 hey bro March, 04 2017 8:31:00
1 3 1 2 hw are u? March, 04 2017 8:32:00
1 4 2 1 good,thnks4asking March, 04 2017 8:33:00
5 1 3 1 r u there? March, 04 2017 8:34:00
5 2 1 3 yup,say March, 04 2017 8:35:00
5 3 3 1 hw are u? March, 04 2017 8:36:00
5 4 1 3 m fine March, 04 2017 8:37:00
5 5 3 1 where are u these days? March, 04 2017 8:38:00
1 5 1 2 your most welcom :D March, 04 2017 8:39:00
Here ID
is the discussion id (chat id say for example 1 is the discussion id between users having id 1 and 2 and 5 is the for discussion between users having id 1 and 3 ) and ID2
is the number in the discussion. The resultant should show 2 messages because "your most welcome" is the last message between users having ID 1 and 2 and the 2nd message "where are u these days?" is the last message between users having ID 1 and 3
I tried many queries like
SELECT message ,MAX(id2)
FROM pm
WHERE user1=1 OR user2=1
GROUP BY id
SELECT message
FROM pm
WHERE user1=1 OR user2=1
GROUP BY id
HAVING id2=MAX(id2)
NOTE: For now I just want the last from the user (having ID=1
) point of view.