MySQL join to return count(report) from all places, even where the number is zero
I have two tables:
countries: province, country, lat, lng
reports: title, province, country, lat, lng
There are thousands of province, country combinations that have no reports, but I have received a reques开发者_开发百科t to print out all data from every province, even if there is no report associated with those provinces, which I would like MySQL to return 0.
I have given the Excel guy,
SELECT province, country, lat, lng, COUNT(report)
FROM reports
GROUP BY lat,lng
Obviously this only gives places that have hits associated with them. However, he said he needs the places that contain 0. I know this must be some sort of 'join' function, but I can't figure out how to do this efficiently.
SELECT c.province, c.country, c.lat, c.lng, COUNT(r.title)
FROM countries c
LEFT JOIN reports r
ON c.province = r.province
AND c.country = r.country
AND c.lat = r.lat
AND c.lng = r.lng
GROUP BY c.province, c.country, c.lat, c.lng
精彩评论