SQL Server NULLABLE 列与 SQL COUNT() 函数

发布于 2024-12-15 16:33:47 字数 655 浏览 0 评论 0原文

有人可以帮助我理解一些事情吗?如果可以的话,我通常会避免在 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)

我可以计算 NameNULL 的记录数,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

咋地 2024-12-22 16:33:47

那是因为

COUNT(column_name) 函数返回值的数量(NULL
指定列的值将不被计算在内

因此当您计算 Id 时,您会得到预期的结果,而计算 Name 则不会,但查询提供的答案是正确的

That's because

The COUNT(column_name) function returns the number of values (NULL
values will not be counted) of the specified column

so when you count Id you get expected result, while counting Name no, but the answer provided by query is correct

短暂陪伴 2024-12-22 16:33:47

COUNT (Transact-SQL)< 中描述了所有内容/a>.

COUNT ( { [ [ ALL | DISTINCT ] expression ] | * } )

ALL - 默认情况下

COUNT(*) 返回组中的项目数。这包括 NULL 值和重复值。

COUNT(ALL expression) 计算组中每一行的表达式,并返回非空值的数量。

Everything is described in COUNT (Transact-SQL).

COUNT ( { [ [ ALL | DISTINCT ] expression ] | * } )

ALL - is default

COUNT(*) 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.

耶耶耶 2024-12-22 16:33:47

“COUNT()”不计算 NULL 值。所以基本上:

SELECT COUNT(Id) FROM Tbl WHERE Name IS NULL

将返回 ("ID" IS NOT NULL) AND ("Name" IS NULL); 的行数;结果为“1”

while:

SELECT COUNT(Name) FROM Tbl WHERE Name IS NULL

将计算其中 (“Name” IS NOT NULL) AND (“Name” IS NULL); 的行;结果永远是0

"COUNT()" does not count NULL values. 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); result is "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

不语却知心 2024-12-22 16:33:47

正如所说,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 use COUNT(1), but actualy you will not see difference in performance.

掩耳倾听 2024-12-22 16:33:47

“永远避免使用 *”是人们盲目遵循的笼统声明之一。如果您知道避免 * 的原因,那么您就会知道在执行 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(*).

醉态萌生 2024-12-22 16:33:47

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 * in COUNT(*) is not the same * in SELECT * FROM...

SELECT COUNT(*) FROM T; very specifically means the cardinality of the table expression 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.

SELECT COUNT(c) FROM T; where c is a nullable column in table T 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文