count(*) vs count(column-name) - 哪个更正确? [英] count(*) vs count(column-name) - which is more correct?
本文介绍了count(*) vs count(column-name) - 哪个更正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果你在这两个例子中使用 count(*)
和 count(column-name)
会有区别吗?
Does it make a difference if you do count(*)
vs count(column-name)
as in these two examples?
我倾向于总是写 count(*)
因为它似乎更适合我的想法,它是一个聚合函数的概念,如果有道理的话.
I have a tendency to always write count(*)
because it seems to fit better in my mind with the notion of it being an aggregate function, if that makes sense.
但我不确定它在技术上是否最好,因为我经常看到没有 *
的示例代码.
But I'm not sure if it's technically best as I tend to see example code written without the *
more often than not.
计数(*):
select customerid, count(*), sum(price)
from items_ordered
group by customerid
having count(*) > 1;
对比计数(列名):
SELECT customerid, count(customerid), sum(price)
FROM items_ordered
GROUP BY customerid
HAVING count(customerid) > 1;
推荐答案
COUNT(*)
计算所有行COUNT(column)
只计算非 NULL 值COUNT(1)
与COUNT(*)
因为 1 是非空表达式COUNT(*)
counts all rowsCOUNT(column)
counts non-NULLs onlyCOUNT(1)
is the same asCOUNT(*)
because 1 is a non-null expressions
您对COUNT(*)
或COUNT(column)
的使用应该基于仅所需的输出.
Your use of COUNT(*)
or COUNT(column)
should be based on the desired output only.
这篇关于count(*) vs count(column-name) - 哪个更正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文