select 班级表.班级id,班级表.班级名称, sum(case when 性别='男' then 1 else 0 end) as 男生总和, sum(case when 性别='女' then 1 else 0 end) as 女生总和 from 班级表 left join 学生表 on 班级表.班级id=学生表.班级id
select 班级表.班级id,班级表.班级名称, sum(case when 性别='男' then 1 else 0 end) as 男生总和, sum(case when 性别='女' then 1 else 0 end) as 女生总和 from 班级表 left join 学生表 on 班级表.班级id=学生表.班级id group by 班级表.班级id,班级表.班级名称
sum(case when 性别='男' then 1 else 0 end) as 男生总和,
sum(case when 性别='女' then 1 else 0 end) as 女生总和
from 班级表
left join
学生表
on 班级表.班级id=学生表.班级id
group by 班级表.班级id,班级表.班级名称