I am creating a report orders by town.
SELECT S.city, count(*) as NumOfOrders
FROM Shop as S
LEFT JOIN orders O ON O.ShopID = S.ShopID
WHERE O.status = 4
Group by S.city
The result display something like this:
Town 1 | 53
Town 2 | 45
Town 3 | 64
It work fine but I want to display all towns even no orders?
Expected Result:
Town 1 | 53
Town 2 | 45
Town 3 | 64
Town 4 | 0
Town 5 | 0
I have tried replacing LEFT JOIN to RIGHT JOIN, that dont even work. Same result.
Though you are using a LEFT JOIN, you are using the o.statu column in a Where Clause and hence the rows with null values (cos of left join) will be dropped.
Try this: