gpt4 book ai didi

MySQL获取2个人之间的最新消息

转载 作者:行者123 更新时间:2023-11-29 04:38:06 26 4
gpt4 key购买 nike

SELECT *
FROM messages
WHERE message_id IN (SELECT MAX(message_id)
FROM messages
GROUP BY if(sender_id > receiver_id, CONCAT(sender_id, receiver_id), CONCAT(receiver_id, sender_id))
)

我有一个简单的消息传递系统,其中的消息是 1 t 1(无组)。我只想获取两个用户之间的最新消息。

上面的“有效”,但我认为我应该使用最大时间戳与 message_id 来获取最新信息。

我的 table 是:

message_id (unique, auto inc),
sender_id (Primary),
receiver_id (Primary),
time_date,
content

最佳答案

你能不能简单地这样做:

Select message_id from messages where sender = x and receiver = y having max (timestamp) group by message_id

感觉即使这不太正确,您也应该能够在此处使用 having 子句并消除对子查询的需要。

关于MySQL获取2个人之间的最新消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36516766/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com