[英]SQL Server NULLABLE column vs SQL COUNT() function
Could someone help me understand something? 有人能帮我理解吗? When I can, I usually avoid (*) in an SQL statement.
当我可以,我通常在SQL语句中避免(*)。 Well, today was payback.
好吧,今天是回报。 Here is a scenario:
这是一个场景:
CREATE TABLE Tbl (Id INT IDENTITY(1, 1) PRIMARY KEY, Name NVARCHAR(16))
INSERT INTO Tbl VALUES (N'John')
INSERT INTO Tbl VALUES (N'Brett')
INSERT INTO Tbl VALUES (NULL)
I could count the number of records where Name
is NULL
as follows: 我可以计算
Name
为NULL
的记录数,如下所示:
SELECT COUNT(*) FROM Tbl WHERE Name IS NULL
While avoiding the (*), I discovered that the following two statements give me two different results: 在避免(*)的同时,我发现以下两个语句给出了两个不同的结果:
SELECT COUNT(Id) FROM Tbl WHERE Name IS NULL
SELECT COUNT(Name) FROM Tbl WHERE Name IS NULL
The first statement correctly return 1 while the second statement yields 0 . 第一个语句正确返回1,而第二个语句产生0 。
Why
or How
? Why
或How
?
That's because 那是因为
The
COUNT(column_name)
function returns the number of values (NULL values will not be counted) of the specified columnCOUNT(column_name)
函数返回指定列的值数(不计算NULL值)
so when you count Id you get expected result, while counting Name no, but the answer provided by query is correct 因此,当您计算Id时,您会得到预期的结果,同时计算名称号,但查询提供的答案是正确的
Everything is described in COUNT (Transact-SQL) . 所有内容都在COUNT(Transact-SQL)中描述 。
COUNT ( { [ [ ALL | DISTINCT ] expression ] | * } )
ALL
- is default ALL
- 是默认值
COUNT(*)
returns the number of items in a group. COUNT(*)
返回组中的项目数。 This includes NULL values and duplicates. 这包括NULL值和重复项。
COUNT(ALL expression)
evaluates expression for each row in a group and returns the number of nonnull values . COUNT(ALL expression)
计算组中每行的表达式,并返回非空值的数量。
"COUNT()" does not count NULL values. “COUNT()”不计算NULL值。 So basically:
所以基本上:
SELECT COUNT(Id) FROM Tbl WHERE Name IS NULL
will return the number of lines where ("ID" IS NOT NULL) AND ("Name" IS NULL); 将返回行数(“ID”IS NOT NULL)AND(“Name”IS NULL); result is "1"
结果是“1”
While: 而:
SELECT COUNT(Name) FROM Tbl WHERE Name IS NULL
will count the lines where ("Name" IS NOT NULL) AND ("Name" IS NULL); 将计算行数(“名称”不为空)和(“名称”为空); result will always be 0
结果将始终为0
As it was said, COUNT (column_name)
doesn't count NULL values. 如前所述,
COUNT (column_name)
不计算NULL值。 If you don't want use COUNT(*)
then use COUNT(1)
, but actualy you will not see difference in performance. 如果你不想使用
COUNT(*)
然后使用COUNT(1)
,但实际上你不会看到性能上的差异。
"Always avoid using *" is one of those blanket statements that people blindly follow. “总是避免使用*”是人们盲目追随的一揽子陈述之一。 If you knew the reasons why you were avoiding * then you would know that none of those reasons apply when doing count(*).
如果你知道你为什么要避免*的原因那么你会知道在计数(*)时这些原因都不适用。
The *
in COUNT(*)
is not the same *
in SELECT * FROM...
在
*
在COUNT(*)
是不一样的*
在SELECT * FROM...
SELECT COUNT(*) FROM T;
very specifically means the cardinality of the table expression T
. 非常具体地表示表格表达式
T
的基数。
SELECT COUNT(1) FROM T;
will generate the same results as COUNT(*)
but if the contents of the parentheses is not *
then it must be parsed. 将生成与
COUNT(*)
相同的结果,但如果括号的内容不是*
则必须解析它。
SELECT COUNT(c) FROM T;
where c
is a nullable column in table T
will count the non-null values. 其中
c
是表T
的可空列,将计算非空值。
PS I'm comfortable with using SELECT * FROM...
in the right circumstances. PS我在适当的情况下使用
SELECT * FROM...
很舒服。
PPS Your 'table' has no key: consider INSERT INTO Tbl VALUES ('John', 'John', 'John', NULL, NULL, NULL);
PPS你的'表'没有键:考虑
INSERT INTO Tbl VALUES ('John', 'John', 'John', NULL, NULL, NULL);
would be allowed by the results would be nonsense. 将被允许的结果将是无稽之谈。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.