I'm coding a private message system, which works perfectly. I need to write a SQL query for group messages, and that message should come up only once. In short I want a "conversation view" just like Facebook, only show the latest message either it is sent or recieved. Script in backend is PHP in private messaging system.
I attached a screen shot:

Here is the attached query I'm using.
Testing demo is uploaded on this address: http://developers89.byethost14.com/messages/
SELECT *
FROM conversation
WHERE (LEAST(sender_ID, receiverID), GREATEST(sender_ID, receiverID), date)
IN
(
SELECT LEAST(sender_ID, receiverID) x,
GREATEST(sende_ID, receiverID) y,
MAX(date) max_date
FROM conversation
GROUP BY x, y
)
AND '$uid' IN (sender_ID, receiverID)
// AND other conditions if you have ...
// ORDER BY ...
// LIMIT ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With