我有一个简单的一对一聊天程序,我想找出两个参与者至少发送过一条消息的讨论。我将如何形成这样的查询?
表格如下(简化):
Table USER
- id
- username
Table DISCUSSION
- id
Table MESSAGE
- id
- user_id (the user who sent the message)
- discussion_id (the discussion this message belongs to)
基本上,我需要检查至少有两个MESSAGE
行具有相同的discussion_id
但是different user_id
。
答案 0 :(得分:1)
SELECT discussion_id FROM message
GROUP BY discussion_id
HAVING count(user_id) > 1
应该这样做。
如果您的讨论表中包含更多列,例如辩论的主题,你可以加入。