mysql count忽略空_MySQL count 过滤空值,使其不将空数据计入条数

首先,看如下两个查询语句:

第一种

SELECT t.id, t.meetStyle, t.title,

count(p.id) AS personCount,

count(f.id) AS fileCount

FROM meeting_info t

LEFT JOIN meeting_person p

ON t.id= p.mId

LEFT JOIN meeting_file f

ON t.id=f.mId where t.ID='info0000000234'

第二种

SELECT t.id, t.meetStyle, t.title,

count(DISTINCT p.id) AS personCount,

count(DISTINCT f.id) AS fileCount

FROM meeting_info t

LEFT JOIN meeting_person p

ON t.id= p.mId

LEFT JOIN meeting_file f

ON t.id=f.mId where t.ID='info0000000234'

这两条语句虽然只有一词之差,实际却相差千里,因为第一条语句的查询结果和如下语句查询结果相同:

SELECt f1.id,f1.MeetStyle,f1.title,f1.personCount,f2.fileCount from

(SELECT t.id, t.meetStyle, t.title,

count(p.id) AS personCount

FROM meeting_info t

LEFT JOIN meeting_person p

ON t.id= p.mId

where t.ID='info0000000234' ) f1

,

(SELECT t.id, t.meetStyle, t.title,

count(f.id) AS fileCount

FROM meeting_info t

LEFT JOIN meeting_file f

ON t.id=f.mId where t.ID='info0000000234') f2

)

由上面两种查询可以看出,count(DISTINCT p.id)具有过滤当数值为空的情况,使其不再计入总数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值