按帖子计数排序作者姓名

时间:2016-04-04 21:33:09

标签: mysql

我有两张桌子。

AUTHOR

Author_ID      Author_Name               
 -------------------------                      
1           name 1      
2           name 2    
3           name 3
 ---------------------------

POST

Post_ID      Author_ID
 -------------------------                      
1           1      
2           1    
3           2
4           3
5           3
6           3
 ---------------------------

我需要一个mysql查询来计算每个作者拥有的帖子数量,然后将作者从最大到最小排序。

我当前的MySQL只显示名称:

select * from AUTHOR
where author_status = '1' ORDER BY author_name DESC

1 个答案:

答案 0 :(得分:1)

SELECT author.author_id, author.author_name, count(post.post_id) FROM AUTHOR, POST
    WHERE (author_status = '1')
    AND (author.author_id = post.author_id) 
GROUP BY author.author_id
ORDER BY author.author_name DESC;