我有表格消息:
我想重新开始对话。对话是当 id_sender 和 id_recipient 以及 id_ad 在行中相同时。但例如:
第一行:id_sender = 1 id_recipient = 2 id_ad = 1
第二行 id_sender = 2 id_recipient = 1 id_ad = 1
这 2 行应该分组到一个对话中,所以我创建查询:
SELECT id_ad
, id_sender
, u.name
, id_recipient
, uu.name
, subject from ads.messages m
JOIN ads.advertisements
USING (id_ad)
JOIN ads.users u
ON m.id_sender = u.id_user
JOIN ads.users uu
ON m.id_recipient = uu.id_user
WHERE m.id_recipient = 133
OR m.id_sender = 133
GROUP
BY id_ad
, id_sender
, subject
, u.name
, m.id_recipient
, uu.name
返回:
此查询是正确的,但我需要返回上图中的结果之一,查询应返回 3 行
返回哪一个并不重要——要重新运行我需要所有这一列,结果行应该是 3 而不是 4
有什么想法吗?
最佳答案
关于mysql - 具有连接和多个分组依据的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37905681/