当前位置: 代码迷 >> Sql Server >> 论坛的排行榜的sql语句解决方案
  详细解决方案

论坛的排行榜的sql语句解决方案

热度:63   发布时间:2016-04-27 13:21:54.0
论坛的排行榜的sql语句
论坛有个帖子表,字段有id(主键)、userid(另外用户表的主键),里面假设数据如下:
1 1
2 1
3 1
4 1
5 2  
6 2  
7 2
8 3
……
如何构建一个sql语句实现根据用户发帖量选取出前10个用户?
求高手指点

------解决方案--------------------
SQL code
--> 测试数据:[tbl]if object_id('[tbl]') is not null drop table [tbl]create table [tbl]([id] int,[userid] int)insert [tbl]select 2,1 union allselect 3,1 union allselect 4,1 union allselect 5,2 union allselect 6,2 union allselect 7,2 union allselect 8,3 union allselect 1,1 union allselect 2,1 union allselect 3,1 union allselect 4,1 union allselect 5,2 union allselect 6,2 union allselect 7,2 union allselect 8,3 union allselect 8,3select top 2 userid,COUNT(1) as times from tbl group by userid order by COUNT(1) desc/*userid    times1    72    6*/
------解决方案--------------------

select top 10 userid,username,count(*)
from a join user on s.userid=user.userid
group by userid,uesrname
order by count(*) desc
  相关解决方案