mysql 5.7 列出当月每日日期
时间: 2023-07-02 18:21:30 浏览: 85
你可以使用 MySQL 的 DATE_FORMAT 函数和 CURDATE 函数来列出当月每日的日期。下面是相应的 SQL 查询语句:
```
SELECT DATE_FORMAT(date, '%Y-%m-%d') AS day
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 d
WHERE MONTH(date) = MONTH(CURDATE())
ORDER BY date;
```
这个查询语句会生成一个数字序列,从当前日期往前数,然后使用 DATE_FORMAT 函数将日期格式化为我们需要的格式(YYYY-MM-DD),最后筛选出当月的日期并按日期排序。
阅读全文