MySQL按另一个表的计数顺序

时间:2011-07-20 04:02:35

标签: php mysql sql

让我说我有:

   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的那些博客)怎么办?

1 个答案:

答案 0 :(得分:4)

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