实现:
select sum(if(a.type=0,a.cou,0)) as type0, sum(if(a.type=1,a.cou,0)) as type1 from (SELECT count(1) as cou,type FROM user GROUP BY type) a
这段sql想要实现的,根据用户类型分组查询出每种类型的人数,并且,type=0的赋值给type0,type=1的赋值给type1。并行显示
1、如果只是分组查询:
SELECT count(1) as cou,type FROM user GROUP BY type
查询出结果为
2、如果以上面sql查询,效果如下: