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

时间:2022-09-25 15:43:03

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

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:

朋友你好我想在这个表中显示每个用户的最后一条消息,假设$ _SESSION ['id']是1,所以我想显示每个用户的最后消息1这里是我的查询:

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

SELECT * FROM消息WHERE(msg_from ='1')或(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.

但是当我运行它时,它会显示来自msg_from列中的用户以及msg_to列中的两条消息,并且它不显示最后插入的消息,请大家,我需要帮助。

2 个解决方案

#1


0  

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

因为你是按MAX(msg_id)DESC订购的,所以你只需要第一个结果,因为它是最新的,那么你可以做类似的事情

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 :)

希望能帮助到你 :)

#2


0  

Fetching the last messages for user-1 from others:

从其他人那里获取user-1的最后消息:

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:

从user-1获取最后的消息给其他人:

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.

我没有使用你的MAX(msg_id)来订购数据,相反,我用日期来获取最新消息。

#1


0  

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

因为你是按MAX(msg_id)DESC订购的,所以你只需要第一个结果,因为它是最新的,那么你可以做类似的事情

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 :)

希望能帮助到你 :)

#2


0  

Fetching the last messages for user-1 from others:

从其他人那里获取user-1的最后消息:

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:

从user-1获取最后的消息给其他人:

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.

我没有使用你的MAX(msg_id)来订购数据,相反,我用日期来获取最新消息。