Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group Private Messaging System

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:

Screenshot

Here is the attached query I'm using.

Testing demo is uploaded on this address: http://developers89.byethost14.com/messages/

like image 910
Waqas Ghouri Avatar asked Jul 03 '26 03:07

Waqas Ghouri


1 Answers

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 ...
like image 193
John Woo Avatar answered Jul 04 '26 18:07

John Woo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!