Hello friends, imagine a table where messages are recorded. I ensure that the last message of users with RecipientID = 1 and the latest incoming message are displayed at the top. So far, no problem. However, when I try to reduce the senders to unique ones to avoid data duplication using Group By, Order By does not work. In short, when a person queries their own messages with RecipientID, and I try to fetch the last messages of other users with SenderID and users, Order By Desc doesn't work. The MYSQL code is as follows:
SELECT * FROM messages
JOIN users ON messages.SenderID = users.UserID
WHERE RecipientID = 1
GROUP BY SenderID
ORDER BY messages.MessageID DESC
LIMIT 10
When Group By SenderID is not added, it lists all incoming messages. What I want to do is to only show the latest incoming messages. I don't understand why Order By doesn't work when Group By is used.
SELECT * FROM messages
JOIN users ON messages.SenderID = users.UserID
WHERE RecipientID = 1
GROUP BY SenderID
ORDER BY messages.MessageID DESC
LIMIT 10
When Group By SenderID is not added, it lists all incoming messages. What I want to do is to only show the latest incoming messages. I don't understand why Order By doesn't work when Group By is used.