I'm creating a message system in PHP but I’m having a bit of a problem with the MySQL query. I want a simple and clean solution that is easy to understand. The query should sort the messages into conversations and then display the username + latest message. There are a few solutions here on the forum but they are all long and complicated and I have a hard time understanding them.
I have the database setup like this:
(messages) id, messages, sentby, sentto
And my query looks like this:
SELECT m1.*
FROM messages m1
LEFT
JOIN messages m2
ON m1.sentby => m2.sentby
AND m1.id < m2.id
WHERE m2.id IS NULL
AND m1.sentto = ?
The query sorts the messages into conversations and displays them in a good order but the last messages on each conversation is not displayed. All the messages have an id so the latest message should be displayed if I sort them by id?
I am one of many beginners here so be friendly and describe the solutions in detail. All the help I can get is appreciated!