SQL查询两个日期之间的所有的日期
时间: 2023-08-29 19:10:04 浏览: 1226
取两个日期之间的所有日期
5星 · 资源好评率100%
你可以使用以下SQL查询来获取两个日期之间的所有日期:
```sql
SELECT DATE_ADD(start_date, INTERVAL seq DAY) AS date
FROM (
SELECT DATE('2022-01-01') AS start_date, DATE('2022-01-10') AS end_date
) AS d
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 UNION ALL
SELECT 10 UNION ALL SELECT 11 UNION ALL SELECT 12 UNION ALL SELECT 13 UNION ALL SELECT 14 UNION ALL
SELECT 15 UNION ALL SELECT 16 UNION ALL SELECT 17 UNION ALL SELECT 18 UNION ALL SELECT 19 UNION ALL
SELECT 20 UNION ALL SELECT 21 UNION ALL SELECT 22 UNION ALL SELECT 23 UNION ALL SELECT 24 UNION ALL
SELECT 25 UNION ALL SELECT 26 UNION ALL SELECT 27 UNION ALL SELECT 28 UNION ALL SELECT 29
) AS seq_0_to_29
WHERE DATE_ADD(start_date, INTERVAL seq DAY) BETWEEN start_date AND end_date;
```
在上面的查询中,你需要将 `'2022-01-01'` 和 `'2022-01-10'` 替换为你想要查询的起始日期和结束日期。这个查询将返回在这两个日期之间的所有日期。
阅读全文