Table items
keeps track of the different types of items that can be stocked.
item_type item_name last_stock 1 cake 2010-08-10 2 fruit 2010-08-07 3 soda 2010-08-07 4 water 2010-08-09
Table individual_items
keeps track of each specific item.
id item_type 1 1 2 2 3 1
My query in MySQL:
SELECT i.item_type, i.item_name, COUNT(j.id)
FROM items i
LEFT OUTER JOIN individual_items j
ON i.item_type = j.item_type
GROUP BY j.item_type
However, the COUNT(j.id)
is screwing with my result. It appears to be grouping any items that are defined but not actually in existence.
item_type item_name COUNT(j.id) 1 cake 2 2 fruit 1 3 soda 0
I think the expected fourth row 4 water 0
is not appearing because COUNT()
is incorrectly grouping the non-existent rows that result from the LEFT OUTER JOIN
. How can I fix this?