在mySQL/SQL中count(0),count(1)..和count(*)之间有什么区别? [英] What is the difference between count(0), count(1).. and count(*) in mySQL/SQL?
问题描述
我最近在一次采访中被问到这个问题. 我在mySQL中进行了尝试,并得到了相同的结果(最终结果). 全部给出了该特定表中的行数. 谁能解释他们之间的主要区别.
I was recently asked this question in an interview. I tried this in mySQL, and got the same results(final results). All gave the number of rows in that particular table. Can anyone explain the major difference between them.
推荐答案
除非您在表中指定字段或在括号内指定表达式而不是常量值或*
Nothing really, unless you specify a field in a table or an expression within parantheses instead of constant values or *
让我给您详细的答案.计数将为您提供给定字段的非空记录号.假设您有一个名为A
Let me give you a detailed answer. Count will give you non-null record number of given field. Say you have a table named A
select 1 from A
select 0 from A
select * from A
将全部返回相同数量的记录,即表A中的行数.输出仍然不同.如果表中有3条记录.以X和Y作为字段名称
will all return same number of records, that is the number of rows in table A. Still the output is different. If there are 3 records in table. With X and Y as field names
select 1 from A will give you
1
1
1
select 0 from A will give you
0
0
0
select * from A will give you ( assume two columns X and Y is in the table )
X Y
-- --
value1 value1
value2 (null)
value3 (null)
因此,所有三个查询都返回相同的数字.除非您使用
So, all three queries return the same number. Unless you use
select count(Y) from A
由于只有一个非空值,您将获得1作为输出
since there is only one non-null value you will get 1 as output
这篇关于在mySQL/SQL中count(0),count(1)..和count(*)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!