Klopt, bedankt
Ik had nog niet eerder met dat UNION gewerkt, maar dat is me nu wel duidelijk geloof ik. Volgens mij is dit nog wat efficiënter:
SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| SELECT
a.titel AS artikel_titel,
c.titel AS categorie_titel
FROM
artikel a
LEFT JOIN artikel_categorie ac ON a.id = ac.artikel_id
LEFT JOIN categorie c ON ac.categorie_id = c.id
UNION DISTINCT
SELECT
a.titel AS artikel_titel,
c.titel AS categorie_titel
FROM
categorie c
LEFT JOIN artikel_categorie ac ON c.id = ac.categorie_id
LEFT JOIN artikel a ON ac.artikel_id = a.id
WHERE a.id IS NULL |
Die WHERE-clause is volgens mij wel "veilig", omdat ik alle a.id's die niet NULL zijn al in de eerste SELECT meeneem.