SQL Server NULLABLE 列与 SQL COUNT() 函数
有人可以帮助我理解一些事情吗?如果可以的话,我通常会避免在 SQL 语句中使用 (*)。嗯,今天算是报应了。这是一个场景:
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)
我可以计算 Name
为 NULL
的记录数,如下所示:
SELECT COUNT(*) FROM Tbl WHERE Name IS NULL
在避免 (*) 的同时,我发现以下两条语句给了我两个不同的结果:
SELECT COUNT(Id) FROM Tbl WHERE Name IS NULL
SELECT COUNT(Name) FROM Tbl WHERE Name IS NULL
第一个语句正确返回1,而第二个语句产生0。 为什么
还是如何
?
Could someone help me understand something? When I can, I usually avoid (*) in an SQL statement. 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:
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. Why
or How
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
那是因为
因此当您计算 Id 时,您会得到预期的结果,而计算 Name 则不会,但查询提供的答案是正确的
That's because
so when you count Id you get expected result, while counting Name no, but the answer provided by query is correct
COUNT (Transact-SQL)< 中描述了所有内容/a>.
ALL
- 默认情况下COUNT(*)
返回组中的项目数。这包括 NULL 值和重复值。COUNT(ALL expression)
计算组中每一行的表达式,并返回非空值的数量。Everything is described in COUNT (Transact-SQL).
ALL
- is defaultCOUNT(*)
returns the number of items in a group. This includes NULL values and duplicates.COUNT(ALL expression)
evaluates expression for each row in a group and returns the number of nonnull values.“COUNT()”不计算 NULL 值。所以基本上:
将返回 ("ID" IS NOT NULL) AND ("Name" IS NULL); 的行数;结果为“1”
while:
将计算其中 (“Name” IS NOT NULL) AND (“Name” IS NULL); 的行;结果永远是0
"COUNT()" does not count NULL values. So basically:
will return the number of lines where ("ID" IS NOT NULL) AND ("Name" IS NULL); result is "1"
While:
will count the lines where ("Name" IS NOT NULL) AND ("Name" IS NULL); result will always be 0
正如所说,
COUNT (column_name)
不计算 NULL 值。如果您不想使用
COUNT(*)
,请使用COUNT(1)
,但实际上您不会看到性能差异。As it was said,
COUNT (column_name)
doesn't count NULL values.If you don't want use
COUNT(*)
then useCOUNT(1)
, but actualy you will not see difference in performance.“永远避免使用 *”是人们盲目遵循的笼统声明之一。如果您知道避免 * 的原因,那么您就会知道在执行 count(*) 时这些原因都不适用。
"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(*).
COUNT(*)
中的*
与SELECT * FROM...
中的*
不同SELECT COUNT(*) FROM T;
非常具体地表示表表达式T
的基数。SELECT COUNT(1) FROM T;
将生成与COUNT(*)
相同的结果,但如果括号中的内容不是*
则它必须被解析。SELECT COUNT(c) FROM T;
其中c
是表T
中可为空的列,将计算非空值。PS 我很乐意在正确的情况下使用
SELECT * FROM...
。PPS 你的“表”没有键:考虑
INSERT INTO Tbl VALUES ('John', 'John', 'John', NULL, NULL, NULL);
结果将是无意义的。The
*
inCOUNT(*)
is not the same*
inSELECT * FROM...
SELECT COUNT(*) FROM T;
very specifically means the cardinality of the table expressionT
.SELECT COUNT(1) FROM T;
will generate the same results asCOUNT(*)
but if the contents of the parentheses is not*
then it must be parsed.SELECT COUNT(c) FROM T;
wherec
is a nullable column in tableT
will count the non-null values.P.S. I'm comfortable with using
SELECT * FROM...
in the right circumstances.P.P.S. Your 'table' has no key: consider
INSERT INTO Tbl VALUES ('John', 'John', 'John', NULL, NULL, NULL);
would be allowed by the results would be nonsense.