mysql 5.7 列出起始日期到截止日期 每日日期
时间: 2023-07-04 20:18:55 浏览: 117
你可以使用 MySQL 的 DATE_FORMAT 函数和一个日期序列生成器来列出起始日期到截止日期之间的每日日期。下面是相应的 SQL 查询语句:
```
SELECT DATE_FORMAT(date, '%Y-%m-%d') AS day
FROM (
SELECT DATE_ADD('起始日期', INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY) AS date
FROM (SELECT 0 AS a 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) AS a
CROSS JOIN (SELECT 0 AS a 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) AS b
CROSS JOIN (SELECT 0 AS a 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) AS c
) AS d
WHERE date BETWEEN '起始日期' AND '截止日期'
ORDER BY date;
```
将上述查询语句中的「起始日期」和「截止日期」替换为实际的起始日期和截止日期即可。这个查询语句同样会生成一个数字序列,然后使用 DATE_FORMAT 函数将日期格式化为我们需要的格式(YYYY-MM-DD),最后筛选出指定日期范围内的日期并按日期排序。
阅读全文