我有一个包含此数据的表格:
我想获得仅由user_id 140和1共享的thread_id。
所以在我的例子中,结果将是356和358.
有了这个请求
select a.user_id , b.user_id, a.thread_id
from `wp_bp_messages_recipients` as a, `wp_bp_messages_recipients` as b
where a.thread_id = b.thread_id
and a.user_id = 1
and b.user_id = 140
我有356,358,359,但我不想要thread_id 359,因为它与user_id 5共享。
非常感谢你的帮助!
答案 0 :(得分:0)
您可以GROUP BY
使用不同的COUNT
,例如:
SELECT thread_id
FROM table_name
WHERE thread_id NOT IN (SELECT thread_id FROM table_name WHERE user_id NOT IN (1, 140))
GROUP BY thread_id
HAVING COUNT(DISTINCT(user_id)) = 2;