一个很简单的SQL语句.求助

来源:百度知道 编辑:UC知道 时间:2024/05/27 14:26:35
表mark字段 值
name zhangsan,lisi,wangwu,zhaoliu,chenqi,dingba
mark 20,30,40,50,60,70
class 1,2,2,3,1,3

我现在想以班级(class)为条件分类查询出总的mark
一条语句是 select sum(mark) from userinfo where class=1;
我现在按team来查询不可能写三条吧?
select sum(mark) from userinfo where class=2,
select sum(mark) from userinfo where class=3

求高手把这3条记录整合为一条记录!

select class , sum(mark)
from userinfo
group by class

select case when class =1 then sum(mark) else 0 end as '一班'
,case when class=2 then sum(mark) else o end as '二班'
,case when class=3 then sum(mark) else o end as '三班'
显示结果为:
|一班-|-二班-|-三班|
|1000-|-500--|-1500|

汇总查询数据的一条sql,用group就可以实现了。
select class,sum(mark) from userinfo group by class

select class,sum(mark) from [userinfo] group by class

SELECT CLASS,SUM(MARK)
FROM USERINFO
GROUP BY CLASS

结果:
CLASS SUM(MARK)
1 80
2 70
3 120

没有理解..
是指一条语句查询 class= 1,2,3 的 sum(mark) 吗?
select sum(mark) from userinfo where class in (1,2,3)