假如存在下面这张表users:
1.通过sql语句返回总人数、男生总数和女生总数
select count(1) '总人数', (select count(1) from users u2 where u2.sex=1) '男生总数', (select count(2) from users u3 where u3.sex=0) '女生总数' from users u1;
2.按照班级统计总人数,总男生人数和女生人数
select u0.class,count(1) '总人数', (select count(1) from users u1 where u1.sex=1 and u1.class=u0.class) '男生总数', (select count(1) from users u2 where u2.sex=0 and u2.class=u0.class) '女生总数' from users u0 group by u0.class;