How do I perform 'WHERE' on groups of rows?
Posted
by
Drew
on Stack Overflow
See other posts from Stack Overflow
or by Drew
Published on 2012-04-15T23:06:29Z
Indexed on
2012/04/15
23:29 UTC
Read the original article
Hit count: 262
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.
© Stack Overflow or respective owner