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
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 班级