假设我有:

   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


这将返回按其帖子数排序的所有博客。
如果我只希望一些博客作者,但仍按相同条件下令怎么办(例如,AUX字段等于3的博客作者)?

最佳答案

WHERE子句应该紧接在GROUP BY子句之前。当对语法等简单明了的内容有疑问时,可以参考手册。

MySQL Manual :: SELECT Syntax

   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

10-02 21:06