mysql count(0),在mySQL / SQL中count(0),count(1)..和count(*)之间有什么区别?

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 *

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

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

since there is only one non-null value you will get 1 as output

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值