3 回答

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊
count(*)計(jì)數(shù)NULL而count(column)不是
[edit]添加了此代碼,以便人們可以運(yùn)行它
create table #bla(id int,id2 int)
insert #bla values(null,null)
insert #bla values(1,null)
insert #bla values(null,1)
insert #bla values(1,null)
insert #bla values(null,1)
insert #bla values(1,null)
insert #bla values(null,null)
select count(*),count(id),count(id2)
from #bla
結(jié)果7 3 2

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
使用*和特定列之間的另一個(gè)細(xì)微差別是,在列情況下,您可以添加關(guān)鍵字DISTINCT,并將計(jì)數(shù)限制為不同的值:
select column_a, count(distinct column_b)from tablegroup by column_ahaving count(distinct column_b) > 1;

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊
另一個(gè)也許是微妙的區(qū)別是,在某些數(shù)據(jù)庫實(shí)現(xiàn)中,count(*)是通過查看有問題的表上的索引而不是實(shí)際的數(shù)據(jù)行來計(jì)算的。由于沒有指定特定的列,因此無需為實(shí)際的行及其值而煩惱(如果您計(jì)算了特定的列,則會如此)。允許數(shù)據(jù)庫使用索引數(shù)據(jù)可能比將其計(jì)為“實(shí)際”行要快得多。
添加回答
舉報(bào)