一个表里面比如有两个这样的字段,一个是班级的id,一个是兴趣小组id,统计每个班级参加各个兴趣小组的人数,sql怎么写

xuwenren 2006-11-21 11:10:55
班级 兴趣小组(1) 兴趣小组(2) 兴趣小组(3)
1 2 3 4
2 5 6 7
...全文
244 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuwenren 2006-11-23
  • 打赏
  • 举报
回复
d
xuwenren 2006-11-23
  • 打赏
  • 举报
回复
请问kinsey(little8),如果还要在case when t.interestid=1 then 1 else 0 end 里面加条件,应该怎么样加????
xuwenren 2006-11-21
  • 打赏
  • 举报
回复
请具体说说
懒得去死 2006-11-21
  • 打赏
  • 举报
回复
拆分表。
kinsey 2006-11-21
  • 打赏
  • 举报
回复
1、假设你的信息存在tb里,包括id,name和interestid
2、假设另外有一个专门存放兴趣小组信息的表interest,表中有ID字段。
则查询语句可以这样写(Mysql5.0测试通过):

select t.id,t.name,
sum(case when t.interestid=1 then 1 else 0 end ) as Group1,
sum(case when t.interestid=2 then 1 else 0 end ) as Group2,
sum(case when t.interestid=3 then 1 else 0 end ) as Group3
from tb as t,interest as i where t.interestid=i.ID group by t.name
kinsey 2006-11-21
  • 打赏
  • 举报
回复
select 班级, sum(case when 兴趣小组=1 then 1 else 0 end ) as Group1,
sum(case when 兴趣小组=2 then 1 else 0 end ) as Group2,
sum(case when 兴趣小组=3 then 1 else 0 end ) as Group3
from 表 where 兴趣小组 in (1,2,3) group by 班级

将其中的中文改为表名何字段名即可
xuwenren 2006-11-21
  • 打赏
  • 举报
回复
班级 兴趣小组(1) 兴趣小组(2) 兴趣小组(3)
1 2 3 4
2 5 6 7


比如说有两个班级,分别是1班和2班,有三个兴趣小组,分别是兴趣小组(1), 兴趣小组(2), 兴趣小组(3),现在要统计1班和2班分别参加这三个兴趣小组的人数,如上面画的一样
wildlily980 2006-11-21
  • 打赏
  • 举报
回复
你的描述很不确切。看不明白。

56,867

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧