Reputation: 7673
If I have the following example table:
id docId tagId
1 12 2
2 13 2
3 13 3
4 13 4
5 14 3
How can I select the docId where the tagId is both 2 and 3, i.e. docId = 13.
Upvotes: 1
Views: 306
Reputation: 2256
use Self Join.
SELECT DISTINCT t1.docId
FROM test AS t1
INNER JOIN test AS t2
ON (t1.docId = t2.docId and t1.tagId =2 AND t2.tagId =3)
Upvotes: 0
Reputation: 135938
select docId
from YourTable
where tagId in (2,3)
group by DocId
having count(distinct tagId) = 2
Upvotes: 2
Reputation: 425833
Assuming that (docId, tagId)
combination is unique:
SELECT docId
FROM mytable
WHERE tagId IN (2, 3)
GROUP BY
docId
HAVING COUNT(*) = 2
Upvotes: 1