Do a RIGHT JOIN
instead. Try this:
SELECT type.id, type.name, COUNT(page.type) AS tot
FROM page
RIGHT JOIN type ON page.type=type.id
GROUP BY type.id
Note the different way of counting. page.type will be NULL
for types that don't have
pages. Such rows will be ignored when doing the COUNT
[EDIT] Here is an example. If we have following data:
type
id name
1 A
2 B
3 C
page
id type
1 1
2 1
3 2
If we do the JOIN
like in my answer (no GROUP BY
or COUNT
), our result set will look like this:
type.id type.name page.id page.type
1 A 1 1
1 A 2 1
2 B 3 2
3 C NULL NULL
Now when we do a GROUP BY
and COUNT(type.id)
, we are counting rows
where type.id is not NULL. That would be all rows: result is 2 for A
and 1 for B and C.
If we do COUNT(page.type)
instead, we get 2 for A, 1 for B and 0 for C (because
page.type
was NULL
for C!)