没有注意过的count(0),count(1),count(*),count(列名)
--创建测试表
create table tb(id varchar(10))
--插入非空数据
insert tb select 'test'
go
--测试
select count(0) as 'count(0)',count(1) as 'count(1)',count(*) as 'count(*)',count(id) as 'count(id)' from tb
--结果
/*
count(0) count(1) count(*) count(id)
1 1 1 1
*/
--插入null值
insert tb values(null)
go
--测试
select count(0) as 'count(0)',count(1) as 'count(1)',count(*) as 'count(*)',count(id) as 'count(id)' from tb
--结果
/*
count(0) count(1) count(*) count(id)
2 2 2 1
*/
--插入空值
insert tb values ('')
go
--测试
select count(0) as 'count(0)',count(1) as 'count(1)',count(*) as 'count(*)',count(id) as 'count(id)' from tb
--结果
/*
count(0) count(1) count(*) count(id)
3 3 3 2
*/
--结论
/*
count(0)=count(1)=count(*) --不忽略null值和空值
count(列名) --忽略null值
*/
create table tb(id varchar(10))
--插入非空数据
insert tb select 'test'
go
--测试
select count(0) as 'count(0)',count(1) as 'count(1)',count(*) as 'count(*)',count(id) as 'count(id)' from tb
--结果
/*
count(0) count(1) count(*) count(id)
1 1 1 1
*/
--插入null值
insert tb values(null)
go
--测试
select count(0) as 'count(0)',count(1) as 'count(1)',count(*) as 'count(*)',count(id) as 'count(id)' from tb
--结果
/*
count(0) count(1) count(*) count(id)
2 2 2 1
*/
--插入空值
insert tb values ('')
go
--测试
select count(0) as 'count(0)',count(1) as 'count(1)',count(*) as 'count(*)',count(id) as 'count(id)' from tb
--结果
/*
count(0) count(1) count(*) count(id)
3 3 3 2
*/
--结论
/*
count(0)=count(1)=count(*) --不忽略null值和空值
count(列名) --忽略null值
*/