I have a little problem with the left join, where I need a list of projects, and for each design I want to display how many comments each construct has.
I use LEFT JOIN
SELECT ds.*, count(com.comment) AS countcom FROM tdic_designs ds LEFT JOIN tdic_comments com ON (com.design_id = ds.id) WHERE ds.approved = 1 AND ds.hidden = 0 AND com.approved = 1 GROUP BY ds.id ORDER BY ds.date_added ASC
But this does not work, since it displays only one project that has 1 comment, but I have two projects in the table where the second design has no comment.
If I changed SQL to
SELECT ds.*, count(com.comment) AS countcom FROM tdic_designs ds LEFT JOIN tdic_comments com ON (com.design_id = ds.id) GROUP BY ds.id, com.approved, ds.approved ORDER BY ds.date_added ASC
This is the removal of the WHERE clause. But this is bad, because he will choose both projects and comments that have not been approved.
What will I miss / do wrong?
source share