mysql生成近30天的所有日期字符串集合
时间: 2023-06-15 08:03:51 浏览: 110
可以使用MySQL的日期函数和子查询来生成近30天的所有日期字符串集合,例如:
```
SELECT DATE_FORMAT(date, '%Y-%m-%d') AS date_str
FROM (
SELECT CURDATE() - 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 dates
WHERE date BETWEEN CURDATE() - INTERVAL 29 DAY AND CURDATE()
ORDER BY date;
```
这个查询会生成当前日期往前的30天内的所有日期字符串集合,格式为`YYYY-MM-DD`。
阅读全文