开发者

MySQL Query: display the number of posts for each unique city

开发者 https://www.devze.com 2022-12-09 00:46 出处:网络
I\'m 开发者_如何学Gonot very good at database queries. Need help with what is probably a simple query.

I'm 开发者_如何学Gonot very good at database queries. Need help with what is probably a simple query.

Database: MYSQL

zipcodes [table]

zip | city | state

post [table]

post_id | title | post | zip

I need to display the number of posts for each unique city.


SELECT count(*) FROM post LEFT JOIN zipcodes ON post.zip = zipcodes.zip GROUP BY city;


I would do it this way. This would display the city name in the output and also show teh cities for which there are no posts exist.

SELECT city,count(*) as "num_posts" FROM zipcodes LEFT JOIN post ON post.zip = zipcodes.zip GROUP BY city;
0

精彩评论

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