场景:当count的字段信息有空值时
- count(1)和count(*)统计数据量时包括null;
- count(column)不统计字段为空的数据;
- count(null)恒为0;
- 当count()的括号中有表达式时,赋予null的数据不统计
表:student
id | name | birth |
---|---|---|
1 | 张三 | 02-01 |
2 | 李四 | 01-01 |
3 | 王五 | null |
4 | 宋六 | null |
例子:
mysql> select count(*) from student;
count(*)
--------
4
mysql> select count(1) from student;
count(1)
--------
4
mysql> select count(birth) from student;
count(*)
--------
2
-- 以下这个案例中,生日为01-01的人数据设置成了null,所以不在统计范围
mysql> select count(case when birth='01-01' then null else birth end) from student;
count(*)
--------
1
--