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 班级将其中的中文改为表名何字段名即可
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(little8),如果还要在case when t.interestid=1 then 1 else 0 end 里面加条件,应该怎么样加????
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 班级将其中的中文改为表名何字段名即可
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