论坛的排行榜的sql语句
论坛有个帖子表,字段有id(主键)、userid(另外用户表的主键),里面假设数据如下:
1 1
2 1
3 1
4 1
5 2
6 2
7 2
8 3
……
如何构建一个sql语句实现根据用户发帖量选取出前10个用户?
求高手指点
[解决办法]
--> 测试数据:[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