Skip to content Skip to sidebar Skip to footer

Mysql Multi-level Parent Selection/join Question

I have a hopefully simple MySQL query question which is eluding me at late at night. I'm trying to do a SELECT which counts the number of instances of a set of data (orders) and g

Solution 1:

Just join them together:

select categoryid, count(orders.id)
from category c
left join product_group pg on pg.category_id = c.id
left join products on p on p.product_group_id = pg.id
left join orders o on o.product_id = p.id

For categories without an order, count(orders.id) will return 0, while count(*) would return one or more, depending on the number of productgroups and products.

An inner join would not count categories without orders at all.

Post a Comment for "Mysql Multi-level Parent Selection/join Question"