From the table below, how would I select all animalIds that have a specific combination of attributes, for example. if I provided attributes 455 and 685, I would expect to return animalIds 55 and 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 reliable way?
SELECT animalId
FROM animalAttributes
WHERE attributeId IN (455,685)
GROUP BY animalId
HAVING COUNT(DISTINCT attributeId) = 2;
source
share