MySQL数据库录入了年月日的日期,获取连续的日期,断开日期跳过
时间: 2023-05-23 18:01:54 浏览: 201
可以使用以下 SQL 语句获取连续的日期,断开日期跳过:
SELECT DATE_ADD(start_date, INTERVAL seq DAY) AS date_sequence
FROM (
SELECT '2020-01-01' AS start_date, 0 AS seq
UNION ALL
SELECT start_date, seq + 1
FROM (
SELECT start_date, seq
FROM (
SELECT MIN(date_field) AS start_date
FROM table_name
) t1
CROSS JOIN (
SELECT 0 AS seq
UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4
UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8
UNION ALL SELECT 9
) t2
) t3
WHERE DATE_ADD(start_date, INTERVAL seq DAY) <= NOW() -- 使用 NOW() 替换为你的结束日期
) t4
LEFT JOIN (
SELECT DISTINCT date_field
FROM table_name
) t5 ON date_sequence = date_field
WHERE date_field IS NULL
ORDER BY date_sequence ASC;
```
其中,`table_name` 是你的 MySQL 数据库表名,`date_field` 是保存日期的列名。你可以将 `'2020-01-01'` 替换为你需要的起始日期,将 `NOW()` 替换为你需要的结束日期。
阅读全文