mysql8新特性CTE递归实现30天时间日期

WITH RECURSIVE dates (date) AS
(
SELECT DATE_SUB(CURDATE(), INTERVAL 30 DAY) FROM DUAL
UNION ALL
SELECT date + INTERVAL 1 DAY FROM dates
WHERE date + INTERVAL 1 DAY <= (SELECT CURDATE() from DUAL) 

)
SELECT * FROM dates ORDER BY dates.date desc ;

 

SELECT DATE_FORMAT(DATE_SUB(NOW(), INTERVAL ac DAY), '%Y-%m-%d') AS DATE
FROM ( 
   SELECT @num:=@num+1 AS ac FROM 
   
    (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5) num1, 
    (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 ) num2,  
   (SELECT @num:=0) num 
) ad

 

https://dev.mysql.com/doc/refman/8.0/en/with.html#common-table-expressions

 

posted @ 2020-12-09 16:45  ZXJ~佳  阅读(370)  评论(0编辑  收藏  举报