1. 程式人生 > >sql中count(0),count(1),count(),count(列名)

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

轉載自:https://blog.csdn.net/zrcode/article/details/73551578

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值
*/