开发者

MySQL join to return count(report) from all places, even where the number is zero

开发者 https://www.devze.com 2023-04-12 15:46 出处:网络
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 receive

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号