个人总结:
'' | null | |
是否占空间 | 否 | 是 |
length值 | 0 | null |
count是否统计 | 是 | 否 |
查询方式 | = '' | is null |
1、''不占空间,null占空间
2、count会统计为 '' 值的记录,不会统计为 null 值的记录
个人建议:
1、使用count时,尽量使用count(*), 不要使用count(列名)或count(常量)
2、在不影响开发情况下,可以使用''代替null,不占空间且查询快(null不能使用索引)
建表、初始化数据
create table pets(
id int primary key auto_increment,
name varchar(20),
kind varchar(20)
);
insert into pets(name, kind) VALUES ('',null);
select * from pets;
length
select *,length(id),length(name),length(kind) from pets;
查询
select * from pets where kind is null;
select * from pets where name = '';
count
select count(*) from pets;
//输出:1
select count(name) from pets;
//输出:1
select count(kind) from pets;
//输出:0