我有一个简单的聊天表:
id, sender_id, sender_name, recipient_id, recipient_name, content
尝试将Person A
(我)和(其他人)或(其他人)和Person A
之间发生的所有聊天分组
怎么会有人这么做呢?
编辑
目标是获得A所在的所有聊天列表,并且只列出其他人。
。如果A与B、C和D聊天,我想显示B、C和D的列表,然后在下一个视图中,我将显示A和其他人的聊天记录。
select * from chat where
recipient_id in (persona, personb )
and sender_id in ( persona, personb )
编辑:从技术上讲,你不是在SQL意义上的"分组",你可能是在排序结果。
像这样:
select id, content, sender_id as other_person
from chat
where recipient_id = persona
union
select id, content, recipient_id
from chat
where sender_id = persona
order by other_person
也请考虑现在就正常化,而不是以后。你"不查询两次"的想法是不正确的。最好不要重复名字
try this
SELECT * FROM chatTable
WHERE sender_name IN('personA','personB') AND
recipient_name IN('personA','personB');