My below SQL query is supposed to display 28 results, however it only displays 14.
I think its something to do with my joins, one of the tables has no records at all for the missing results but I expect it to display as NULL if thats the case:
SELECT c.Name, u.Id, u.From, u.Send, COUNT(al.Id) as Total
FROM Auction_Location al
LEFT JOIN US u ON u.Id = al.CId
LEFT JOIN City c ON c.Id = u.CityId
WHERE al.CityId IN (983, 984)
GROUP BY c.Name, u.Id, u.From, u.Send
ORDER BY c.Name ASC;
Your WHERE
clause is turning the outer join into an inner join. Move the condition to an ON
clause:
SELECT b.Name, b.Id, b.First, b.Second, COUNT(c.Id) as Total
FROM Table1 a LEFT JOIN
Table2 b
ON b.Id = a.MyId LEFT JOIN
Table3 c
ON c.Id = b.CId AND c.Id IN (9, 10)
GROUP BY b.Name, b.Id, b.First, b.Second
ORDER BY b.Name ASC;
Note that I changed the COUNT()
so it counts matches in table3
rather than table1
. And I fixed the SELECT
and GROUP BY
clauses so they match.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.