查詢數據,沒有數據補0,日期必須出現,通常用戶圖表數據查詢

SELECT IFNULL(b.count,0) AS `order`
    FROM (
        SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS click_date
        UNION ALL
        SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS click_date
        UNION ALL
        SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS click_date
        UNION ALL
        SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS click_date
        UNION ALL
        SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS click_date
        UNION ALL
        SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS click_date
        UNION ALL
        SELECT CURDATE() AS click_date
    ) a LEFT JOIN (
      SELECT DATE(e.`payTime`) AS `datetime`, COUNT(*) AS COUNT
       FROM `edu_eduorder` e LEFT JOIN `edu_course` ec ON e.`cid`= ec.`tid` WHERE e.`status`=2 AND ec.`instituId`=#{instituId}
      GROUP BY DATE(e.`payTime`) DESC
    ) b ON a.click_date = b.datetime
相關文章
相關標籤/搜索