我的表格的日期字段因月而异,例如:
id import_date name ...
1 2017-02-01 Leonardo
7 2017-03-01 Alison
8 2017-06-01 Juliet
11 2017-09-01 Laura
每月的第一天,我们会将一些数据导入此表。如您所见,在此示例中,缺少1月,4月,5月,7月和8月的某些注册。
如何才能获得开始日期和结束日期范围之间缺少的日期?
答案 0 :(得分:0)
这就是我所做的:
SELECT data_series.import_date FROM
(
SELECT DATE_ADD(:startDate, INTERVAL @i :=@i + 1 MONTH) AS import_date
FROM imports, (SELECT @i := -1) i,
(SELECT MIN(import_date) as minVal, MAX(import_date) as maxVal
FROM imports WHERE import_date BETWEEN :startDate AND :endDate) AS smallerSet
WHERE @i < DATEDIFF(smallerSet.maxVal, smallerSet.minVal)
) AS data_series
LEFT JOIN
(
SELECT DISTINCT import_date FROM imports
WHERE reference_date BETWEEN :startDate AND :endDate
) AS import_period
ON data_series.import_date = import_period.import_date
WHERE import_period.import_date IS NULL
其中:startDate
和:endDate
是我在Spring Boot中使用的变量。