Sunday, 14 September 2014

Find Last Message FROM All Users

 Query to find out the last message from all users in conversation

select messages.message_text, messages.sender_uuid, messages.receiver_uuid, messages.add_date  FROM messages 
SELECT  MAX(add_date) AS time, IF(`receiver_uuid` = '1',  `sender_uuid`, `receiver_uuid`) AS user FROM     dp_messages GROUP BY user) m2 
ON messages .add_date = m2.time 
AND (messages .`sender_uuid` = m2.user  OR dp_messages .`receiver_uuid` = m2.user)

`sender_uuid` = '1' OR `receiver_uuid` = '1' 
ORDER BY messages .add_date DESC LIMIT 0, 20

1 comment: