SQL - How To Order Using Count From Another Table

后端 未结 6 1590
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 04:39

1. Bloggers

blogger_id
1 
2
3

2. Posts

post_from_blogger_id
1 
1
1
2
2
3

As

6条回答
  •  一整个雨季
    2021-01-31 05:04

     SELECT bloggers.*, COUNT(post_id) AS post_count
        FROM bloggers LEFT JOIN blogger_posts 
        ON bloggers.blogger_id = blogger_posts.blogger_id
        GROUP BY bloggers.blogger_id
        ORDER BY post_count
    

    (Note: MySQL has special syntax that lets you GROUP BY without aggregating all values, it's intended for exactly this situation).

提交回复
热议问题