So a rough structure of my two tables concered is as follows:
Table one: Services
services.id
services.name
Table two: Orders
orders.id
orders.item
orders.service
Table one contains a list of services. And table two is a list of orders. I am trying to generate a list of all orders for services from table two (orders) but also include (Zero) the services that haven’t been ordered. I’m aware that that’s where the LEFT JOIN comes but it doesn’t seem to be working at all. It displays most of the services but there’s one or two records (from services) not being displayed. Here’s the query i’m using so far..
Any guidance at all is much appreciated, thanks!
select services.name,count(orders.service)
from services
LEFT JOIN orders ON services.id=orders.service
WHERE item IN (1,2,3,4)
group by statuses.service;
Your original selection is fine and should allow all records from the services table through. However you’re then restricting this by your
whereclause. If there was no join for a specific row,itemwill have aNULLin it which yourWHEREclause is filtering out.Forgive me if slightly wrong, I’m coming from SQL Server background.