Say I have a table matching person_id
s to pet
s. I have a list of the ideal pets that a homeowner must have (at least one of each) and, from the following table, I want to see who meets the requirements.
That list is, of course, (dog, cat, tiger)
. People can definitely have more than one of each, but these are essential (therefore person_id = 1
is the only one that works).
+---------+-----------+--------+
| home_id | person_id | pet |
+---------+-----------+--------+
| 1 | 1 | dog |
| 2 | 1 | dog |
| 3 | 1 | cat |
| 4 | 1 | tiger |
| 5 | 2 | dog |
| 6 | 2 | cat |
| 7 | 3 | <null> |
| 8 | 4 | tiger |
| 9 | 4 | tiger |
| 10 | 4 | tiger |
+---------+-----------+--------+
I've been able to check who has a tiger or a cat by running:
select person_id, pet
from house
group by person_id having pet in ('dog','cat','tiger'),
but obviously this gives the person_id
s that have at least one of those pets – not all of them.