一个总数排序的问题,请大家帮忙看看!
有两张表
t1,t2
表t1的字段为:
id 自动编号
username nvarchar(50) 用户名
表t2的字段:
id 自动编号
title nvarchar(50) 标题
userid 对应用户id
数据为:
t1
id username
1 a
2 b
3 c
t2
id title userid
1 lalala 1
2 momomo 1
3 dedede 2
4 kokoko 3
5 kokoko 3
6 kokoko 3
7 kokoko 3
我需要得出记录title内容按多到少的排序,并且显示出数目和用户名
比如上面的例子应该得到这样的查询结果
username counts
c 4
a 2
b 1
请问这样的查询语句怎么写呢??请大家帮忙看看!
------解决方案--------------------select a.id,count(b.userid) counts from t1 a inner join t2 b on a.id=b.userid group by a.id order by count(b.userid) desc
------解决方案--------------------select username,count(username) counts from t1 Inner Join t2 On t1.id=t2.userid group by username order by counts desc
------解决方案--------------------select a.username,count(b.userid) as counts
from t1 a
inner join t2 b on a.id=b.userid
group by a.username
order by count(b.userid) desc