JOIN
समाधान:
SELECT t.*
FROM topics t
JOIN tags_topics t1 ON (t.id = t1.topicId AND t1.tagId = 1)
JOIN tags_topics t2 ON (t.id = t2.topicId AND t2.tagId = 2)
JOIN tags_topics t3 ON (t.id = t3.topicId AND t3.tagId = 3)
GROUP BY
समाधान:
ध्यान दें कि आपको सभी t.*
. को सूचीबद्ध करने की आवश्यकता है GROUP BY
. में कॉलम खंड, जब तक आप MySQL या SQLite का उपयोग नहीं करते।
SELECT t.*
FROM topics t JOIN tags_topics tt
ON (t.id = tt.topicId AND tt.tagId IN (1,2,3))
GROUP BY t.id, ...
HAVING COUNT(*) = 3;
सबक्वायरी समाधान:
SELECT t.*
FROM topics t
WHERE t.id = ANY (SELECT topicId FROM tags_topics tt WHERE tt.tagId = 1)
AND t.id = ANY (SELECT topicId FROM tags_topics tt WHERE tt.tagId = 2)
AND t.id = ANY (SELECT topicId FROM tags_topics tt WHERE tt.tagId = 3);
संशोधित GROUP BY
समाधान:
GROUP BY
को सरल करता है एक सबक्वेरी में खोज को अलग करके क्लॉज।
SELECT t.*
FROM topics t
WHERE t.id IN (
SELECT tt.topicId FROM tags_topics tt
WHERE tt.tagId IN (1,2,3))
GROUP BY tt.id HAVING COUNT(*) = 3
);