I have a table called participants with the following fields:
The background is that there ar
select conversation_id
from participants
where user_id in (1,2)
group by conversation_id
having count(user_id) = 2
Return two users conversation_id
Using your query will not work since the where
clause filters out the user_ids. Use
SELECT * FROM participants
GROUP BY conversation_id
HAVING sum(user_id not in (1,2)) = 0
user_id not in (1,2)
returns 1
if a user_id
other than 1,2
are in a conversation and 0
otherwise. So using SUM
you can add up all that cases. If none are found then the sum is 0
.