简体   繁体   English

count(*)和count(column_name),差异是什么?

[英]count(*) and count(column_name), what's the diff?

count(*)count(column_name) ,mysql的区别是什么。

  • COUNT(*) counts all rows in the result set (or group if using GROUP BY). COUNT(*)计算结果集中的所有行(如果使用GROUP BY,则计算组)。
  • COUNT(column_name) only counts those rows where column_name is NOT NULL. COUNT(column_name)仅计算column_name为NOT NULL的行。 This may be slower in some situations even if there are no NULL values because the value has to be checked (unless the column is not nullable). 在某些情况下,即使没有NULL值,这可能会更慢,因为必须检查该值(除非该列不可为空)。
  • COUNT(1) is the same as COUNT(*) since 1 can never be NULL. COUNT(1)COUNT(*)相同,因为1永远不能为NULL。

To see the difference in the results you can try this little experiment: 要查看结果的差异,您可以尝试这个小实验:

CREATE TABLE table1 (x INT NULL);
INSERT INTO table1 (x) VALUES (1), (2), (NULL);
SELECT
    COUNT(*) AS a,
    COUNT(x) AS b,
    COUNT(1) AS c
FROM table1;

Result: 结果:

a   b   c
3   2   3

根据列定义 - 如果您的列允许NULL - 您可能会得到不同的结果(在Mark已经告知的某些情况下,它可能会慢于count(列))。

There is no performance difference between COUNT (*) , COUNT (ColumnName) , COUNT (1) . COUNT (*)COUNT (ColumnName)COUNT (1)之间没有性能差异。

Now, if you have COUNT (ColumnName) then the database has to check if the column has a NULL value, and NULL s are eliminated from aggregates. 现在,如果您有COUNT (ColumnName)那么数据库必须检查列是否具有NULL值,并且从聚合中消除NULL So COuNT (*) or COUNT (1) is preferable to COUNT (ColumnName) unless you want COUNT (DISTINCT ColumnName) 所以COuNT (*)COUNT (1)最好COUNT (ColumnName) ,除非你想COUNT (DISTINCT ColumnName)

In most cases there's little difference, and COUNT(*) or COUNT(1) is generally preferred. 在大多数情况下,差异很小,通常首选COUNT(*)COUNT(1) However, there's one important situation where you must use COUNT(columnname) : outer joins. 但是,有一个重要的情况是必须使用COUNT(columnname) :outer join。

If you're performing an outer join from a parent table to a child table, and you want to get zero counts in rows that have no related items in the child table, you have to use COUNT(column in child table) . 如果您正在从父表执行外连接到子表,并且您希望在子表中没有相关项的行中获得零计数,则必须使用COUNT(column in child table) When there's no matches, that column will be NULL , and you'll get the desired zero count (actually, you'll get NULL , but you can convert that to 0 with IFNULL() or COALESCE() ). 当没有匹配时,该列将为NULL ,并且您将获得所需的零计数(实际上,您将获得NULL ,但您可以使用IFNULL()COALESCE()将其转换为0 )。 If you use COUNT(*) , it counts the row from the parent table, so you'll get a count of 1 . 如果你使用COUNT(*)它计算从父表中的行,所以你会得到的计数1

SELECT c.name, COALESCE(COUNT(o.id), 0) AS order_count
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么count(column_name)的行为如此? - Why is count(column_name) behaving like this? mysql where count(column_name)= 1? - mysql where count(column_name) = 1? 为什么 HAVING 可以引用 COUNT(column_name) 而不能引用 column_name 本身? - Why can HAVING reference COUNT(column_name) but not the column_name itself? MySql 如何 COUNT(column_name = 'foobar' or null) 工作? - MySql how COUNT(column_name = 'foobar' or null) works? 使用以pk分组并具有count(distinct column_name)= 2的column_name IN('one','two')的查询返回空 - query using column_name IN ('one','two') grouped by pk and having count (distinct column_name) = 2 returns empty MySQL:在列列表中以及在HAVING子句中再次使用COUNT(column_name)。 这会导致COUNT(column_name)操作运行两次吗? - MySQL: Using COUNT(column_name) in the column list, and again in the HAVING clause. Does this cause the COUNT(column_name) operation to run twice? MYSQL中与COUNT(`column`)的蒙古式等效是什么? - What's the mongoid equvilent to COUNT(`column`) in MYSQL? 使用MySQL在column_name之后移动column_name - Move column_name AFTER Column_name using MySQL 为什么column_name =(选择column_name)起作用,而column_name = null不起作用 - Why does column_name = (select column_name) work and not column_name=null 将 column_name 与列数据匹配 - Match column_name to Column data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM