mysql count 没有用0_利用mysql左右连接解决count函数无法统计展示字段值为0的问题...

整张表的表结构:test表

ad56c29bc696fb0e1b5d8516a7a6f726.png

使用select type,count(type) as total from test where remain>5 group by type;发现无法将total值为0的字段展示出来

截图:

23ced90a6f0c9920515fdf5e0943c07a.png

接下来我们利用左右连接解决此问题:

命令:

select distinct type from test;

截图:

93d4f4d205632298e2015242441444ec.png

命令:

select type,count(type) as total from test where remain>5 group by type;

截图:

2e144c73f43ee9ec96b6d361fa860e90.png

命令:

select type,count(type) as total from test where remain>5 group by type;

select * from (select distinct type from test) a

LEFT JOIN

(select type,count(type) as total from test where remain>5 group by type) b

on a.type=b.type;

截图:

6c53afc9fd33b791f4a99846514fce5a.png

完美的将此问题解决,是不是非常的nice?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值