如何解决mysql的count()函数条件表达式不生效问题 |
示例
select count(age = 10) as count from student
原因count(‘任意内容’)都会统计出所有记录数 因为count只有在遇见null时才不计数 即: count(null)==0 解决方法1count()函数中条件表达式加上 select count(age = 10 or null) as count from student 方法2使用 select count(IF(age = 10,1,null)) as count from student 方法3使用 select count(case when age = 10 then 1 else null end) as count from student 总结以上为个人经验,希望能给大家一个参考,也希望大家多多支持 。 |