获取对话列表查询

时间:2016-06-23 19:45:09

标签: mysql sql database

假设我们的表具有以下结构,其中样本数据和其他用户不仅仅是2

enter image description here

所以在这种情况下,我应该只收到每两个用户之间的一次对话,比如

35比1和 1至35

但我尝试了下面的查询,我得到了两个对话

select msg_id,msg_from,msg_to,msg_content,msg_status,msg_created_at,
    (select user_name from users where user_id = msg_from ) user_name 
    from messages where msg_id in 
    (select max(msg_id) from messages ) 
    and msg_to = 35 or msg_from = 35

请帮助以获得正确的结果。

2 个答案:

答案 0 :(得分:1)

您忘记了PunnerToUInt32 x = {13, 42.0F}; auto y = reinterpret_cast<PunnerToUInt32*>(&x.ui32); 语句周围的括号:

or

答案 1 :(得分:1)

我认为你应该将条件msg_to = 35 or msg_from = 35置于括号之间并将其移动到返回max id的子查询中。如下面的代码所示:

select msg_id,msg_from,msg_to,msg_content,msg_status,msg_created_at,
(select user_name from users where user_id = msg_from ) user_name 
from messages where msg_id in 
(select max(msg_id) from messages  
where (msg_to = 35 or msg_from = 35))
相关问题