没有注意过的count(0),count(1),count(*),count(列名)

--创建测试表
create table tb(id varchar(10))
--插入非空数据
insert tb select 'test'
go
--测试
select count(0as 'count(0)',count(1as '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(0as 'count(0)',count(1as '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(0as 'count(0)',count(1as '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值
*/
posted @ 2009-08-11 12:06  czperfectaction  阅读(746)  评论(0编辑  收藏  举报