WmZilla - Webmaster and Marketplace

The Next Generation Webmaster and Trade Forum

Order By does not work when Group By is added in MYSQL

Ph4ntom

New member

0

0%

Status

Offline

Posts

30

Likes

0

Rep

0

Bits

160

3

Months of Service

0%
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.
 

249

6,622

6,642

Top