I have a table, which looks like:
+-----------+----------+
+ person_id + group_id +
+-----------+----------+
+ 1 + 10 +
+ 1 + 20 +
+ 1 + 30 +
+ 2 + 10 +
+ 2 + 20 +
+ 3 + 10 +
+-----------+----------+
I need a query such that only person_ids with groups 10 AND 20 AND 30 are returned (only person_id: 1). I am not sure how to do this, as from what I can see it would require me to group the rows by person_id and then select the rows which contain all group_ids.
I’m looking for something which will preserve the use of keys without resorting to string operations on group_concat() or such.
This is what you’re looking for:
Although efficient, using this solution the amount of values in the
inwill have to match the value you compare the count with.And the just-for-fun solution, as you said, even with
group_concatyou can solve this 😛