SQL Count Problem
Posted: Tue Aug 08, 2006 6:26 pm
When you are using a COUNT with a GROUP BY, how do you make it so this returns 0 if what you're grouping by doesn't have anything in the condition.
For example:
SELECT p.categoryid, COUNT(p.categoryid) FROM products p, product_categories pc WHERE p.categoryid = pc.categoryid GROUP BY p.categoryid;
Right now this returns the following if no products have a category of 1.
categoryid count
2 4
3 10
4 9
I need it to return the category 1 with 0 count.
Sorry if I've explained this horribly... its late in the day!
For example:
SELECT p.categoryid, COUNT(p.categoryid) FROM products p, product_categories pc WHERE p.categoryid = pc.categoryid GROUP BY p.categoryid;
Right now this returns the following if no products have a category of 1.
categoryid count
2 4
3 10
4 9
I need it to return the category 1 with 0 count.
Sorry if I've explained this horribly... its late in the day!