首页  >  文章  >  数据库  >  mysql 组函数count的具体用法

mysql 组函数count的具体用法

WBOY
WBOY原创
2016-06-06 09:45:551082浏览

mysqlcount

SELECT d.department,
COUNT(CASE e.sex WHEN e.sex='f' AND p.position !='主管' THEN p.id END) fq,
COUNT(CASE e.sex WHEN e.sex='f' AND p.position ='主管' THEN p.id END) fz,
COUNT(CASE e.sex WHEN e.sex='m' AND p.position !='主管' THEN p.id END) mq,
COUNT(CASE e.sex WHEN e.sex='m' AND p.position ='主管' THEN p.id END) mz,
COUNT(e.id) xj
FROM department d LEFT JOIN employ e ON d.id=e.deptid
LEFT JOIN position p ON e.positionid=p.id
GROUP BY d.id;

出现后面两个count统计不出来是什么原因?
为什么下面个可以查出来?
SELECT d.department,
COUNT(CASE WHEN e.sex='f' AND p.position !='主管' THEN p.id END) fq,
COUNT(CASE WHEN e.sex='f' AND p.position ='主管' THEN p.id END) fz,
COUNT(CASE WHEN e.sex='m' THEN CASE WHEN p.position !='主管' THEN p.id END END) mq
COUNT(CASE WHEN e.sex='m' THEN CASE WHEN p.position ='主管' THEN p.id END END) mz,
COUNT(e.id) xj
FROM department d LEFT JOIN employ e ON d.id=e.deptid
LEFT JOIN position p ON e.positionid=p.id
GROUP BY d.id

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn