I need to select a conversation_id from a MySQL table where the users in a conversation is the exact match of users provided in an array such as (2, 5) - which should return 1 as the conversation id.
Extract of the db table as follows:
user_ids (2, 5)
conversation_users:
conversation_id user_id
1 2
1 5
2 2
2 6
Can this be done in a single query?
EDIT - 26.06.2014
Actually, after doing some testing this does not always work correctly. If there are more users that belong to a conversation than there are in the array, it will still return all the conversations those users belong to.
I would like to get an exact match of users in the array to users in the conversation.
In the example below, conversations 6 and 7 are both returned but it should not return any rows:
SELECT `conversation_id` FROM `conversation_user` WHERE `user_id` IN (70, 426)
GROUP BY `conversation_id` HAVING COUNT(DISTINCT `user_id`) = 2;
conversation_user_id conversation_id user_id
14 6 70
15 6 29
16 6 442
17 6 425
18 6 426
19 7 70
20 7 442
21 7 426
22 7 499
23 7 425
24 7 29