Get last conversation in mysql table in facebook style

Get last conversation in mysql table in facebook style :

If you want to get conversation(chat) of users ie . the last message in the conversation use the following query it will give the last message in the conversation :
suppose you have following chat table

Get last conversation in mysql table in facebook style

mysql chat sytem | Get user convrsation in chat table

SELECT 
*
FROM (

SELECT *
FROM chat AS c
WHERE c.from = '138'
OR c.to = '138'
ORDER BY c.id DESC
) AS m1
WHERE m1.is_active !=0
GROUP BY LEAST( m1.from, m1.to ) , GREATEST( m1.from, m1.to )
ORDER BY m1.id DESC
LIMIT 0 , 10

Where c.to=2 or c.from=2 is current user’s id .
And is_active is deletion status.


Advertisements

Add Comment

📖 Read More