if语句
案例:同样统计男女生人数,语句如下:
SELECT school,COUNT(school) 人数,SUM(IF(sex='男',1,0)) 男生数,SUM(IF(sex='女',1,0)) 女生数, SUM(IF(pass=1,1,0)) pass1,SUM(IF(pass=0,1,0)) pass0, SUM(IF(state=1,1,0)) state1,SUM(IF(state=0,1,0)) state0 FROM sch GROUP BY sex,school
if语句
案例:同样统计男女生人数,语句如下:
SELECT school,COUNT(school) 人数,SUM(IF(sex='男',1,0)) 男生数,SUM(IF(sex='女',1,0)) 女生数, SUM(IF(pass=1,1,0)) pass1,SUM(IF(pass=0,1,0)) pass0, SUM(IF(state=1,1,0)) state1,SUM(IF(state=0,1,0)) state0 FROM sch GROUP BY sex,school