I have the following MYSQL query:
SELECT categories.key, categories.name, count(*) as itemsCount
FROM categories
LEFT JOIN designs
ON categories.key = designs.category
GROUP BY categories.name ASC
It does the job of selecting all categories and counting the # of designs that belong to each category. However, my app and the DB are set so that if the “category” field in the “designs” table has a value of “0”, then this design belong to the virtual (there is no category with key = 0 in the “categories” table) “unassigned” category.
My question is… is it possible to modify my query to also count designs that do NOT belong to any of the existing categories?
Perhaps I am over-complicating things and it’s much easier to add a category with a “0” key, but for the sake of learning, perhaps there is a better way?
Thanks.
1 Answer