I et af følgende tilfælde, hvis du ikke kender PROJECT.num
/PROJECT_TO_TAGS.project_id
, bliver du nødt til at deltage i PROJECTS
tabel for at få id-værdien for at finde ud af, hvilke tags den har tilknyttet.
Brug af IN
SELECT p.*
FROM PROJECTS p
JOIN PROJECTS_TO_TAGS pt ON pt.project_id = p.num
WHERE pt.tag_id IN (SELECT x.tag_id
FROM PROJECTS_TO_TAGS x
WHERE x.project_id = 4)
Brug af EXISTS
SELECT p.*
FROM PROJECTS p
JOIN PROJECTS_TO_TAGS pt ON pt.project_id = p.num
WHERE EXISTS (SELECT NULL
FROM PROJECTS_TO_TAGS x
WHERE x.project_id = 4
AND x.tag_id = pt.tag_id)
Brug af JOINS (dette er den mest effektive!)
DISTINCT
er nødvendig, fordi JOINs risikerer, at duplikerede data dukker op i resultatsættet...
SELECT DISTINCT p.*
FROM PROJECTS p
JOIN PROJECTS_TO_TAGS pt ON pt.project_id = p.num
JOIN PROJECTS_TO_TAGS x ON x.tag_id = pt.tag_id
AND x.project_id = 4