我有一个像这样的表,我正在尝试获取chat_id,其中成员是excat匹配的,例如我想获取成员1,2的聊天id,但是什么时候这样做Select * member_to_chat WHERE member_id IN (1,2)
它返回结果并来自聊天号 3,这是错误的,因为在这个聊天中我需要 3 个人,如果我给member_id 1和2只得到 chat_id 1,是否可以这样做用mysql,先谢谢你了!如果我的英语不好的话,我很抱歉。
成员(member)聊天
id | member_id | chat_id
1 1 1
2 2 1
----------------------
3 1 2
4 3 2
----------------------
5 1 3
6 2 3
7 3 3
给定member_id 1,2的结果 - chat_id必须为1,或者如果我通过1,2,3必须返回chat_id 3,再次感谢您的任何建议
最佳答案
select distinct chat_id
from member_to_chat m
where
not exists (select chat_id
from member_to_chat
where member_id not in(1,2)
and chat_id=m.chat_id)
and exists
(select chat_id
from member_to_chat
where member_id=1)
and exists
(select chat_id
from member_to_chat
where member_id=2)
关于Mysql查询,获取准确值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389319/