日期:2014-05-18  浏览次数:20613 次

求个sql语句,谢谢
表A
id     userid       f   ...
1           1             1  
2           1             1  
3           2             0  
4           2             1  
5           3             1
...
表B
userid   username   ...
    1               aa
    2               bb
    3               cc
...
实现表C
username   count_f
    aa                 2
    bb                 1
    cc                 1
就是要按表A里字段f=1的多少排序
排出userid的弄出来了,可要从表B里再把username弄出来搞了半天都没搞好~~
先谢谢了

------解决方案--------------------
select b.username,sum(A.f) as count_f from A,B where a.userid=b.userid group by b.username
------解决方案--------------------
select b.username, count(*) count_f
from a , b
where a.userid = b.userid and a.f = 1
group by b.username
order by count_f desc
------解决方案--------------------
Select username,(Select Sum(f) From A Where userid=B.userid) As count_f From B