Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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.
select docId from YourTable where tagId in (2,3) group by DocId having count(distinct tagId) = 2
Add a comment
Assuming that (docId, tagId) combination is unique:
(docId, tagId)
SELECT docId FROM mytable WHERE tagId IN (2, 3) GROUP BY docId HAVING COUNT(*) = 2
SELECT docId FROM my_table WHERE tagId >= 2 AND tagId <= 3;
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)
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.