MySql教程

mysql查询最近12天的数据,没有数据自动补0

本文主要是介绍mysql查询最近12天的数据,没有数据自动补0,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
select a.date_time as dateTime,ifnull(b.order_num,'0.00') as orderNum, ifnull(b.payment_amount,'0.00') as paymentAmount
from ( <include refid="all_date"/> ) a 
left join (
   select date(create_date) as date_time, order_num, payment_amount
   from base_company_daily_data where company_id = #{companyId}
) b on a.date_time = b.date_time
 order by dateTime asc

某一天没有数据,结果中没有显示那天的数据,解决

<sql id="all_date">
        SELECT date_sub(curdate(), interval 0 day) as date_time
        union all SELECT date_sub(curdate(), interval 1 day) as date_time
        union all SELECT date_sub(curdate(), interval 2 day) as date_time
        union all SELECT date_sub(curdate(), interval 3 day) as date_time
        union all SELECT date_sub(curdate(), interval 4 day) as date_time
        union all SELECT date_sub(curdate(), interval 5 day) as date_time
        union all SELECT date_sub(curdate(), interval 6 day) as date_time
        union all SELECT date_sub(curdate(), interval 7 day) as date_time
        union all SELECT date_sub(curdate(), interval 8 day) as date_time
        union all SELECT date_sub(curdate(), interval 9 day) as date_time
        union all SELECT date_sub(curdate(), interval 10 day) as date_time
        union all SELECT date_sub(curdate(), interval 11 day) as date_time
</sql>

 

参考https://blog.csdn.net/ouyang111222/article/details/77638826

这篇关于mysql查询最近12天的数据,没有数据自动补0的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!