数据库
首页 > 数据库> > 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

标签:12,sub,mysql,curdate,time,date,数据,day,SELECT
来源: https://www.cnblogs.com/person008/p/15813670.html