mysql查询补齐12个月_MySQL查询12个月数据,无数据补0

该博客介绍了如何使用MySQL查询来获取一年中每个月的里程数总和,对于没有数据的月份填充0。提供的SQL代码示例以2018年为基准,通过CASE语句按月进行条件判断并求和。
摘要由CSDN通过智能技术生成

1.数据库  和表就用自己的,下面是代码

select

case month(shi_com_time) when '1' then sum(mileage) else 0 end as 一月份,

case month(shi_com_time) when '2' then sum(mileage) else 0 end as 二月份,

case month(shi_com_time) when '3' then sum(mileage) else 0 end as 三月份,

case month(shi_com_time) when '4' then sum(mileage) else 0 end as 四月份,

case month(shi_com_time) when '5' then sum(mileage) else 0 end as 五月份,

case month(shi_com_time) when '6' then sum(mileage) else 0 end as 六月份,

case month(shi_com_time) when '7' then sum(mileage) else 0 end as 七月份,

case month(shi_com_time) when '8' then sum(mileage) else 0 end as 八月份,

case month(shi_com_time) when '9' then sum(mileage) else 0 end as 九月份,

case month(shi_com_time) when '10' then sum(mileage) else 0 end as 十月份,

case month(shi_com_time) when '11' then sum(mileage) else 0 end as 十一月份,

case month(shi_com_time) when '12' then sum(mileage) else 0 end as 十二月份

from bu_waybill

where year(shi_com_time)='2018';

二、执行结果,希望能帮到你

25e7f61e1787?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

2018-03-08  xiaomage

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值