因此,我想做的是检索一个人在其消息传递窗口中看到的所有“初始”消息的列表
这是表结构
thread_id | sender | receiver | message | date | sender_deleted | sender_received | read
xRdaQ | bTP5n | lCBNA | hello! | date | 0 | 0 |
xRdaQ | lCBNA | bTP5n | hey! | date | 0 | 0 |
1T4xR | bTP5n | An03R | hhi | date | 0 | 0 |
我到目前为止尝试过的查询:
select * from messages where sender = 'bTP5n'
union select * from messages where receiver = 'bTP5n'
group by conversation_id
而且我仍然得到两行具有相同的thread_id
与这个查询相同:
select * from messages where sender = 'bTP5n'
union select * from messages where receiver = 'bTP5n'
group by conversation_id order by date desc
他们两个都没有返回我想要的东西,这都是唯一的thread_id,其中发送者或接收者等于“ bTP5n”
免责声明:虚拟数据用于此问题
最佳答案
如果要在第二个联合查询中使用分组依据,则它仅适用于第二个查询,如果要应用所有结果,则必须在所有结果之外编写分组依据。
请尝试以下查询:
select * from
(select * from messages where sender = 'bTP5n'
union
select * from messages where receiver = 'bTP5n'
)
as a group by conversation_id order by date desc