首页 新闻 赞助 找找看

sql语句问题

0
悬赏园豆:5 [已解决问题] 解决于 2011-03-11 07:44

以下表数据:

StuNo             StuName

1                     a

2                     b

3                     a

4                     a

5                     b

6                     c

要求查询的结果是:

a的个数 b的个数 c的个数

3           2           1

请问sql语句怎么写了??情大家帮忙

yixin841210的主页 yixin841210 | 小虾三级 | 园豆:1138
提问于:2011-03-10 16:55
< >
分享
最佳答案
0

select StuName,sum(case StuName when 'a' then 1 else 0 end) as 'a的个数',
sum(case StuName when 'b' then 1 else 0 end) as 'b的个数',
sum(case StuName when 'c' then 1 else 0 end) as 'c的个数' from table
group by StuName

收获园豆:5
飒沓如流星 | 初学一级 |园豆:5 | 2011-03-10 18:21
恩,试了一下,不对呀,还是显示3行,不过还是谢谢
yixin841210 | 园豆:1138 (小虾三级) | 2011-03-10 18:26
select sum(case StuName when 'a' then 1 else 0 end) as 'a的个数',
sum(case StuName when 'b' then 1 else 0 end) as 'b的个数',
sum(case StuName when 'c' then 1 else 0 end) as 'c的个数' from Table_1
飒沓如流星 | 园豆:5 (初学一级) | 2011-03-10 22:29
这样可以了
飒沓如流星 | 园豆:5 (初学一级) | 2011-03-10 22:29
@飒沓如流星:恩,好了,谢谢
yixin841210 | 园豆:1138 (小虾三级) | 2011-03-11 07:43
其他回答(2)
0

select Count(*) from table group by stuname

Lonfee | 园豆:210 (菜鸟二级) | 2011-03-10 16:58
不对呀,你的结果是这样的:
3
2
1
我需要在一行显示的
支持(0) 反对(0) yixin841210 | 园豆:1138 (小虾三级) | 2011-03-10 17:13
0

select max(num), avg(num), min(num)
from
(
select count(stuname) as num from student group by stuname
) as u

mr liao | 园豆:212 (菜鸟二级) | 2011-03-10 22:24
恩,谢谢
支持(0) 反对(0) yixin841210 | 园豆:1138 (小虾三级) | 2011-03-11 07:44

乱写这位

支持(0) 反对(0) 彬彬@科比 | 园豆:43 (初学一级) | 2012-06-01 16:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册