让我假装我有以下“消息”表
ID (auto) Sender Receiver Msg Date (datetime)
1 Dave John Hi 01/01/2013
2 John Dave Hello 02/01/2013
3 James Dave U there? 02/02/2013
4 Dave James Yup 02/03/2013
5 Dave Simon Hey 02/03/2013
我想要Dave正在按照日期订购的人员名单。
Simon
James
John
我不知道如何在不查询“Dave”的情况下对它们进行分组。
非常感谢。
答案 0 :(得分:3)
您似乎想要与Dave交谈的 distinct 人员列表:
select (case when receiver = 'Dave' then Sender else Receiver end)
from messages m
where 'Dave' in (Receiver, Sender)
group by (case when receiver = 'Dave' then Sender else Receiver end)
order max(date) desc;
答案 1 :(得分:-1)
这将给你每个人Dave已发送消息并收到按日期订购的消息。
SELECT DISTINCT(Receiver) FROM messages WHERE Sender = 'Dave' ORDER BY date DESC
答案 2 :(得分:-1)
SELECT Receiver FROM messages WHERE Sender = "Dave"
AND Receiver NOT IN (SELECT Sender FROM messages WHERE Receiver = "Dave")
UNION
SELECT Sender FROM messages WHERE Receiver = "Dave"