需求是:有个查询最近7天的折线图,这个就要求没有数据就要补0
现在表名为:wo_work_order
时间字段为:create_time
SQL如下:
SELECT
DATE_FORMAT( a.timeDay, '%Y-%m-%d' ) AS day,
IFNULL( b.count, 0 ) AS count
FROM
(
SELECT
curdate( ) AS timeDay UNION ALL
SELECT
date_sub( curdate( ), INTERVAL 1 DAY ) AS timeDay UNION ALL
SELECT
date_sub( curdate( ), INTERVAL 2 DAY ) AS timeDay UNION ALL
SELECT
date_sub( curdate( ), INTERVAL 3 DAY ) AS timeDay UNION ALL
SELECT
date_sub( curdate( ), INTERVAL 4 DAY ) AS timeDay UNION ALL
SELECT
date_sub( curdate( ), INTERVAL 5 DAY ) AS timeDay UNION ALL
SELECT
date_sub( curdate( ), INTERVAL 6 DAY ) AS timeDay
) a
LEFT JOIN (
SELECT
date( d.create_time ) AS day,
count(1) AS count
FROM
wo_work_order d
-- 在这里进行条件判断
WHERE d.status = '关闭'
GROUP BY
date( d.create_time )
) b ON a.timeDay = b.day
ORDER BY
day
结果输出如下: