MYSQL 如何获取当月每一天
时间: 2024-04-30 09:22:21 浏览: 131
可以使用 MySQL 中的 DATE_FORMAT 和 CURDATE 函数来获取当月每一天的日期。
以下是一个示例查询,查询当前月份的每一天的日期:
```
SELECT DATE_FORMAT(date, '%Y-%m-%d') AS day
FROM (
SELECT CURDATE() - INTERVAL (DAY(CURDATE())-1) DAY + INTERVAL n DAY AS date
FROM (
SELECT a.N + b.N * 10 + 1 n
FROM (
SELECT 0 AS N
UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6
UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) a, (
SELECT 0 AS N
UNION SELECT 1 UNION SELECT 2 UNION SELECT 3
UNION SELECT 4 UNION SELECT 5 UNION SELECT 6
UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
) b
ORDER BY n
) numbers
WHERE date_format(date, '%Y-%m') = date_format(CURDATE(), '%Y-%m')
) days;
```
这将返回如下结果:
```
+------------+
| day |
+------------+
| 2022-09-01 |
| 2022-09-02 |
| 2022-09-03 |
| 2022-09-04 |
| 2022-09-05 |
| 2022-09-06 |
| 2022-09-07 |
| 2022-09-08 |
| 2022-09-09 |
| 2022-09-10 |
| 2022-09-11 |
| 2022-09-12 |
| 2022-09-13 |
| 2022-09-14 |
| 2022-09-15 |
| 2022-09-16 |
| 2022-09-17 |
| 2022-09-18 |
| 2022-09-19 |
| 2022-09-20 |
| 2022-09-21 |
| 2022-09-22 |
| 2022-09-23 |
| 2022-09-24 |
| 2022-09-25 |
| 2022-09-26 |
| 2022-09-27 |
| 2022-09-28 |
| 2022-09-29 |
| 2022-09-30 |
+------------+
```
阅读全文