SQL - How To Order Using Count From Another Table

后端 未结 6 1589
佛祖请我去吃肉
佛祖请我去吃肉 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:09

    try LEFT JOIN for this question

    SELECT DISTINCT(Bloggers.blogger_id),
    (select  count(post_from_blogger_id) from Posts 
    where Posts.post_from_blogger_id=Bloggers.blogger_id) post_from_blogger_id FROM Bloggers 
    LEFT OUTER JOIN Posts ON Bloggers.blogger_id=Posts.post_from_blogger_id 
    ORDER BY post_from_blogger_id DESC
    

提交回复
热议问题