215

我有以下查询:

select column_name, count(column_name)
from table
group by column_name
having count(column_name) > 1;

如果我替换所有对count(column_name)to 的调用会有什么不同count(*)

这个问题的灵感来自如何在 Oracle 的表中找到重复值?.


为了澄清接受的答案(也许是我的问题),替换count(column_name)count(*)将在结果中返回一个额外的行,其中包含 a和列中值null的计数。null

4

12 回答 12

244

count(*)计算 NULL 并且count(column)不计算

[编辑] 添加此代码以便人们可以运行它

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

结果 7 3 2

于 2008-09-12T15:28:15.127 回答
37

使用 * 和特定列之间的另一个小区别是,在列的情况下,您可以添加关键字 DISTINCT,并将计数限制为不同的值:

select column_a, count(distinct column_b)
from table
group by column_a
having count(distinct column_b) > 1;
于 2008-09-12T15:47:56.407 回答
18

另一个可能是细微的区别是,在某些数据库实现中,count(*) 是通过查看相关表上的索引而不是实际数据行来计算的。由于没有指定特定的列,因此无需担心实际的行及其值(如果您计算了特定的列,就会出现这种情况)。允许数据库使用索引数据比计算“真实”行要快得多。

于 2008-09-14T03:13:10.137 回答
13

文档中的解释有助于解释这一点:

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

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

所以 count(*) 包含空值,而另一种方法不包含。

于 2008-09-12T15:41:27.297 回答
10

我们可以使用Stack Exchange Data Explorer通过一个简单的查询来说明差异。Stack Overflow 数据库中的 Users 表中的列通常留空,例如用户的网站 URL。

-- count(column_name) vs. count(*)
-- Illustrates the difference between counting a column
-- that can hold null values, a  'not null' column, and  count(*)

select count(WebsiteUrl), count(Id), count(*) from Users

如果您在Data Explorer中运行上面的查询,您会看到计数是相同的count(Id)count(*)因为该Id列不允许null值。但是WebsiteUrl,计数要低得多,因为该列允许null.

于 2010-06-14T17:19:38.517 回答
2
  • COUNT(*) 语句指示 SQL Server 返回表中的所有行,包括 NULL。
  • COUNT(column_name) 只检索行上具有非空值的行。

请参阅以下代码以执行 SQL Server 2008:

-- Variable table
DECLARE @Table TABLE
(
      CustomerId int NULL 
    , Name nvarchar(50) NULL
)

-- Insert some records for tests
INSERT INTO @Table VALUES( NULL, 'Pedro')
INSERT INTO @Table VALUES( 1, 'Juan')
INSERT INTO @Table VALUES( 2, 'Pablo')
INSERT INTO @Table VALUES( 3, 'Marcelo')
INSERT INTO @Table VALUES( NULL, 'Leonardo')
INSERT INTO @Table VALUES( 4, 'Ignacio')

-- Get all the collumns by indicating *
SELECT  COUNT(*) AS 'AllRowsCount'
FROM    @Table

-- Get only content columns ( exluce NULLs )
SELECT  COUNT(CustomerId) AS 'OnlyNotNullCounts'
FROM    @Table
于 2012-02-16T19:45:41.593 回答
2

基本上,该COUNT(*)函数返回表中的所有行,而COUNT(COLUMN_NAME)没有;也就是说,它排除了这里的每个人也在这里回答的空值。但最有趣的部分是使查询和数据库优化,COUNT(*)除非进行多次计数或复杂查询,否则最好使用它而不是COUNT(COLUMN_NAME). 否则,在处理大量数据时,它确实会降低您的数据库性能。

于 2013-05-04T20:47:38.187 回答
2

COUNT(*)– 返回表中的记录总数(包括 NULL 值记录)。

COUNT(Column Name) – 返回非 NULL 记录的总数。这意味着,它忽略了计算该特定列中的 NULL 值记录。

于 2019-03-24T19:14:29.980 回答
0

进一步详细说明@SQLMeance 和@Brannon 使用GROUP BYOP 提到但@SQLMenace 的回答中没有出现的子句给出的答案

CREATE TABLE table1 ( 
id INT 
);
INSERT INTO table1 VALUES 
(1), 
(2), 
(NULL), 
(2), 
(NULL), 
(3), 
(1), 
(4), 
(NULL), 
(2);
SELECT * FROM table1;
+------+
| id   |
+------+
|    1 |
|    2 |
| NULL |
|    2 |
| NULL |
|    3 |
|    1 |
|    4 |
| NULL |
|    2 |
+------+
10 rows in set (0.00 sec)
SELECT id, COUNT(*) FROM table1 GROUP BY id;
+------+----------+
| id   | COUNT(*) |
+------+----------+
|    1 |        2 |
|    2 |        3 |
| NULL |        3 |
|    3 |        1 |
|    4 |        1 |
+------+----------+
5 rows in set (0.00 sec)

在这里,COUNT(*)计算每种类型id包括的出现次数NULL

SELECT id, COUNT(id) FROM table1 GROUP BY id;
+------+-----------+
| id   | COUNT(id) |
+------+-----------+
|    1 |         2 |
|    2 |         3 |
| NULL |         0 |
|    3 |         1 |
|    4 |         1 |
+------+-----------+
5 rows in set (0.00 sec)

这里,COUNT(id)计算每种类型的出现次数,id但不计算出现的次数NULL

SELECT id, COUNT(DISTINCT id) FROM table1 GROUP BY id;
+------+--------------------+
| id   | COUNT(DISTINCT id) |
+------+--------------------+
| NULL |                  0 |
|    1 |                  1 |
|    2 |                  1 |
|    3 |                  1 |
|    4 |                  1 |
+------+--------------------+
5 rows in set (0.00 sec)

这里,只COUNT(DISTINCT id)统计每种类型的出现次数id(不计算重复),也不统计出现的次数NULL

于 2021-08-16T07:41:40.023 回答
-1

最好使用

Count(1) in place of column name or * 

计算表中的行数,它比任何格式都快,因为它永远不会去检查表中的列名是否存在

于 2011-02-07T19:12:49.323 回答
-2

如果您的表中固定一列,则没有区别,如果您想使用多于一列,则必须指定需要计算多少列......

谢谢,

于 2012-09-21T12:06:47.273 回答
-2

正如前面的答案中提到的,Count(*)甚至计算NULL列,而count(Columnname)仅当列有值时才计算。

最好的做法是避免*( Select *, count *, ...)

于 2016-05-02T16:50:05.063 回答