仅显示每个用户的最后一条消息

enter image description here

Hello friends i want to display the last message from each user in this table, lets assume the $_SESSION['id'] is 1 so i want to display the last messages by each from or to user 1 here is my query:

SELECT * FROM message WHERE (msg_from='1') OR (msg_to='1') GROUP BY msg_from,msg_to ORDER BY MAX(msg_id) DESC

but when i run this it displays two messages from a user that is in the msg_from column and also in the msg_to column and it doesn't display the last inserted message, please guys, I need help.

since you are ordering by MAX(msg_id) DESC then you reqire only the first result because it will be the latest then you may do something like

SELECT TOP 1 * FROM message where msg_id IN (SELECT msg_id FROM message WHERE (msg_from='1') OR (msg_to='1') GROUP BY msg_from,msg_to ORDER BY MAX(msg_id) DESC);

or

SELECT * FROM message where msg_id IN (SELECT msg_id FROM message WHERE (msg_from='1') OR (msg_to='1') GROUP BY msg_from,msg_to ORDER BY MAX(msg_id) DESC) LIMIT 1;

hope it helps :)

Fetching the last messages for user-1 from others:

SELECT * FROM message WHERE msg_to = '1'
GROUP BY msg_from ORDER BY msg_date DESC

Fetching the last messages from user-1 to others:

SELECT * FROM message WHERE msg_from = '1'
GROUP BY msg_to ORDER BY msg_date DESC

I didn't use your MAX(msg_id) for ordering the data, instead, I used the date to get the latest messages.