forslag til forbedringer
hejjeg har mm tabel med emner og kategorier
antager vi at et bestemt emne (med id 123) har fire kategorier (jazz, rock, ballet, soul) og jeg ønsker at finde alle andre emner der har netop tre af disse så er jeg kommer frem til en SQL der ser ud som følger:
SELECT a.uid_local FROM emne_cat_mm WHERE
1=1 AND
(
(
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = jazz)
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = rock)
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = ballet)
) OR (
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = soul)
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = rock)
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = ballet)
) OR (
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = soul)
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = jazz)
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = ballet)
) OR (
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = soul)
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = jazz)
AND EXISTS (SELECT * FROM emne_cat_mm b WHERE a.uid_local = a.uid_local AND uid_foreign = rock)
)
)
AND a.uid_local NOT IN (123)
Måske en lidt kluntet SQL, men den virker... men er der nogen der har et forslag til optimering af koden?