May 15, 2024

Real Estate Sign

Where Dreams Come Home

How can I get list of users to display in Inbox ordered by the recent person sent the message using SQL & PHP

I’ve got this table by the name messages which contains the sender id and receiver id

What I want to do is fetch all the users I am supposed to display in the inbox list. based on who received or sent the last message in that conversation with the current user id given

Here’s my fiddle

This is what I tried so far which isn’t working as required

SELECT * FROM messages WHERE message_type = 'individual' AND sent_by = 1 GROUP BY receiver_ids 

UNION 

SELECT * FROM messages WHERE message_type = 'individual' AND receiver_ids = 1 GROUP BY sent_by

I want the records to be grouped by user id based on sent or received messages and ordered in timestamp descending order