简体   繁体   English

MySQL 按另一个表的计数排序

[英]MySQL order by count of another table

Let's say I have:假设我有:

   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

That returns all bloggers ordered by their post count.这将返回按帖子数排序的所有博主。 What if I want just some bloggers but still ordered by the same criteria (for example those whose AUX field is equal to 3)?如果我只想要一些博主,但仍然按照相同的标准排序(例如 AUX 字段等于 3 的博主)怎么办?

The WHERE clause should come immediately before the GROUP BY clause. WHERE子句应紧接在GROUP BY子句之前。 When in doubt about something as straightforward as syntax, the place to look is the manual.当对语法这样简单的东西有疑问时,可以查看手册。

MySQL Manual:: SELECT Syntax MySQL 手册:: SELECT 语法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM