gpt4 book ai didi

mysql - 留言讨论查询

转载 作者:行者123 更新时间:2023-11-29 07:18:33 28 4
gpt4 key购买 nike

我尝试创建一个讨论系统,使用 SQL 按时间排序显示两个用户之间的消息,例如,如果用户 10 想查看他与用户 45 的消息,这是我的表结构:

消息( id(PK)、sender_id(FK_USER)、receiver_id(FK_USER)、主题、内容、created_at、updated_at);

我尝试了这个 sql 查询,但它没有显示我想要的内容:

select s.id,
s.subjet,
d.source_id as `source`,
d.id,
d.subject
from messages s,
messages d
where s.destination_id=d.source_id
and d.source_id=s.destination_id
and s.source_id=202
order by created_at asc;

Example

最佳答案

如果我正确理解您的表结构和要求,您可以尝试如下 -

SELECT *
FROM MESSAGES
WHERE SENDER_ID IN (10, 45)
AND RECEIVER_ID IN (10, 45)
ORDER BY CREATED_AT

或者像这样 -

SELECT *
FROM MESSAGES
WHERE (SENDER_ID = 10 AND RECEIVER_ID = 45)
OR (RECEIVER_ID = 10 AND SENDER_ID = 45)
ORDER BY CREATED_AT

默认情况下,排序将以 ASC 方式完成,因此无需提及。

关于mysql - 留言讨论查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37005634/

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