I have a sql table which include conversation between users. I need to retrieve the last message from every conversation in order to preview it.
id | sender | receiver | message | date
1 | 1 | 2 | Hello | 2015-12-08 20:00
2 | 2 | 1 | Hey | 2015-12-08 20:10
3 | 2 | 1 | You there? | 2015-12-08 21:00
4 | 1 | 3 | Yes | 2015-12-08 21:15
5 | 4 | 1 | Hey buddy | 2015-12-08 22:00
I know many similar question on site but i couldn't fix this.
I tried this code but not working properly:
SELECT *
FROM messages
WHERE receiver = '{$id}'
GROUP BY sender
ORDER BY id DESC
LIMIT 10;