Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL LEFT JOIN with COUNT(*) problem

I have the following query:

SELECT products_categories.categoryID, name, COUNT(*) AS itemCount
FROM products_categories
LEFT JOIN products_to_categories ON products_to_categories.categoryID = products_categories.categoryID
GROUP BY products_categories.categoryID

But still there's a problem: categories with no products in them return itemCount = 1 instead of 0. How can I fix this?

like image 701
naburi Avatar asked Feb 27 '23 11:02

naburi


1 Answers

Have you tried COUNT(products_to_categories.categoryID) AS itemCount? I am not really sure but would think that maybe the problem lies in the COUNT(*).

like image 81
Florian Grell Avatar answered Mar 02 '23 19:03

Florian Grell