Given table below

id | groupid | memberid
1  | g1      | m1
2  | g1      | m2
3  | g2      | m1
4  | g2      | m2
5  | g2      | m3

I'm trying to build a query where I want to get the groupid containing exact list of memberids. For example :

Given input as m1, m2 -> return g1

Given input as m1 -> return nothing

Given input as m1, m2, m3 -> return g3

Assuming it to be a simple social network group table, I'm looking to find a group id containing exact list of member ids.

What I did :

I tried building the query using group by, having, count operators but wasn't able to.


1 Answer 1

SELECT groupid
FROM table
HAVING GROUP_CONCAT(DISTINCT memberid ORDER BY memberid) = 'm1,m2'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.