I am trying to list product variations with their quantities ordered, BUT ALSO show the product variations where there is no quantity ordered. So I thought it would be as simple as selecting the products and doing a left join on the orders of each product where the order is a current revision.
So I expected like this order of operations:
SELECT p.product_id, SUM(po.quantity)
FROM `products` p
LEFT JOIN `product_orders` po ON p.product_id=po.product_id
LEFT JOIN `orders` o ON o.order_id=po.order_id AND o.is_current='1'
but that is getting also the quantities where the is_current is not 1
Then I thought, okay, I can just do an inner join after the left join instead like this:
SELECT p.product_id, SUM(po.quantity)
FROM `products` p
LEFT JOIN `product_orders` po ON p.product_id=po.product_id
INNER JOIN `orders` o ON o.order_id=po.order_id AND o.is_current='1'
but then the products which have not been ordered yet are not being listed. I expected them to to show up as SUM(quantity) being NULL.
Can anyone see where my logic has gone wrong?
Thanks!
Scott
If the only product orders that count are those where it is current then you need to find that subset before you do a left join to it. Otherwise if you do a left join you either get all or only those ordered as you’ve discovered.
So something like the following should work: