MYSQL获取当月每一天
时间: 2024-05-06 16:19:38 浏览: 140
可以使用以下语句获取当月每一天的日期:
```
SELECT DATE_FORMAT(date, '%Y-%m-%d') AS day FROM (
SELECT CURDATE() - INTERVAL (DAY(CURDATE())-1) DAY + INTERVAL (n-1) DAY AS date
FROM (
SELECT 1 n UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10
UNION SELECT 11 UNION SELECT 12 UNION SELECT 13 UNION SELECT 14 UNION SELECT 15
UNION SELECT 16 UNION SELECT 17 UNION SELECT 18 UNION SELECT 19 UNION SELECT 20
UNION SELECT 21 UNION SELECT 22 UNION SELECT 23 UNION SELECT 24 UNION SELECT 25
UNION SELECT 26 UNION SELECT 27 UNION SELECT 28 UNION SELECT 29 UNION SELECT 30 UNION SELECT 31
) t
WHERE DATE_ADD(CURDATE(), INTERVAL 1 MONTH) > DATE_ADD(CURDATE() - INTERVAL (DAY(CURDATE())-1) DAY, INTERVAL (n-1) DAY)
) days;
```
该语句的核心是使用一个子查询生成一个包含当月所有日期的临时表,然后在外部查询中将日期格式化为字符串,并将其存储在名为“day”的列中。最终,您将获得一个包含当月每一天的日期的结果集。
阅读全文