sql中count(0),count(1),count(),count(列名)

来源:互联网 发布:毁灭战士4优化 贴吧 编辑:程序博客网 时间:2024/05/20 05:28
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值
*/
原创粉丝点击