I have two tables, Subject and Content, where Content references Subject with foreign key. I want to display how many times each subject appears in Content table (0, if it does not appear). But the query below only gives me the rows with count > 0 and ignores the others:
SELECT Subject.id, Subject.name, COUNT(Content.subject_id) AS `count`
FROM Subject LEFT JOIN Content
ON Subject.id = Content.subject_id
WHERE type = @type
GROUP BY Subject.id;
I checked and tried to follow this, this and this post but for some reason the code above does not work.
Any ideas?
Edit:
the type field is in the Content table and that was causing the the problem as “Will A” pointed out
Which table is
typea column in? I’m supposing that it’s Content – and by including the field in the WHERE clause, you’re forcing the Content table on the right-hand side of the LEFT JOIN to have data in it (which means the LEFT JOIN is actually then just an INNER JOIN).Try: