From the table below, how would I select all animalIds that have a specific combination of attributeIds e.g. if I supplied attributeIds 455 & 685 I'd expect to get back animalIds 55 & 93
Table name: animalAttributes
id attributeId animalId
1 455 55
2 233 55
3 685 55
4 999 89
5 455 89
6 333 93
7 685 93
8 455 93
I have the following query that seems to work, however, I'm not sure if there is a more robust way?
SELECT animalId
FROM animalAttributes
WHERE attributeId IN (455,685)
GROUP BY animalId
HAVING COUNT(DISTINCT attributeId) = 2;
HAVING COUNT(DISTINCT attributeId) = 2