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

SELECT
  click_date,
  count(plan_order_id) num
FROM
  (
      SELECT
        date_sub(curdate(), INTERVAL 1 DAY) AS click_date
      UNION ALL
        SELECT
          date_sub(curdate(), INTERVAL 2 DAY) AS click_date
        UNION ALL
          SELECT
            date_sub(curdate(), INTERVAL 3 DAY) AS click_date
          UNION ALL
            SELECT
              date_sub(curdate(), INTERVAL 4 DAY) AS click_date
  ) a
  LEFT JOIN t_plan_order tpo on tpo.plan_order_date = a.click_date and tpo.is_delete = 0
GROUP BY
  a.click_date

结果如下

mysql查询最近4天的数据,没有数据自动补0_第1张图片

你可能感兴趣的:(mysql)