Can anybody help me with this query ?
I have 3 tables : orders, customers and products.
I need to have a list of the number of orders for each customer + for each product.
Like this:
Customer A Product X 4
Customer A Product Y 0
Customer A Product Z 0
Customer B Product X 2
Customer B Product Y 0
Customer B Product Z 1
Customer C Product X 0
Customer C Product Y 0
Customer C Product Z 8
I tried a query like this :
SELECT c.Name, p.Name, COUNT(o.OrderID)
FROM orders AS o
RIGHT JOIN customers AS c ON c.CustomerID=o.CustomerID
RIGHT JOIN products AS p ON p.ProductID=o.ProductID
GROUP BY c.Name, p.Name
but I can’t get it to work !
It only displays the combinations where the counter>0 (where there are records in ‘orders’). But with only 1 join it DOES work, and then it DOES correctly display the records with a counter of 0. (In this example there are no products Y sold, but I do want to see Y in the list of combinations)
Any ideas?
Use a cross join. This is Oracle SQL, so not sure if it’ll work for mysql.