1

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.

4 Answers 4

2
select docId
    from YourTable
    where tagId in (2,3)
    group by DocId
    having count(distinct tagId) = 2
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that (docId, tagId) combination is unique:

SELECT  docId
FROM    mytable
WHERE   tagId IN (2, 3)
GROUP BY
        docId
HAVING  COUNT(*) = 2

Comments

0
SELECT docId
FROM my_table
WHERE tagId >= 2
AND tagId <= 3;

Comments

0

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) 

Comments

Your Answer

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.

Ask question

Explore related questions

See similar questions with these tags.