2个表,做个统计,排序,情况如下
a表
id,item
1,1
2,1
3,2
2,1
1,2
2,2
.....b表id,name
1,bb
2,cc
3,dd
.....
要求结果,统计a并降序排序,结果如下:
cc,4
bb,3
dd,2
小弟刚上路,在线等。。急
a表
id,item
1,1
2,1
3,2
2,1
1,2
2,2
.....b表id,name
1,bb
2,cc
3,dd
.....
要求结果,统计a并降序排序,结果如下:
cc,4
bb,3
dd,2
小弟刚上路,在线等。。急
FROM B LEFT JOIN A
OB A.ID=B.ID
GROUP BY B.NAME
ORDER BY SUM(A.Item) DESC
from a join b on a.id=b.id
group by b.name
order by sum(a.item) desc
select b.name,sum(a.item) from a inner join b on (a.id=b.id) group by b.name order by sum(a.item) desc
create table tb_a(id int,item int)
insert into tb_a
select 1,1 union all
select 2,1 union all
select 3,2 union all
select 2,1 union all
select 1,2 union all
select 2,2create table tb_b (id int,name varchar(5))
insert into tb_b
select 1,'bb' union all
select 2,'cc' union all
select 3,'dd'select * from tb_a
select * from tb_bselect name,isnull(SUM(a.item),0)
from tb_b b left join tb_a a on b.id=a.id
group by name
order by isnull(SUM(a.item),0) descname (无列名)
cc 4
bb 3
dd 2