获取近30天日期
SELECT DATE_FORMAT( DATE_SUB( now(), INTERVAL ac day ), '%Y%m%d' ) AS time FROM ( SELECT @ai := @ai + 1 AS ac FROM ( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 ) ac1, ( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 ) ac2, ( SELECT @ai := 0 ) xc0 ) ad
SELECT DATE_FORMAT( DATE_SUB( DATE_SUB(curdate(),INTERVAL -1 DAY), INTERVAL ac DAY ), '%Y%m%d' ) AS endTime FROM ( SELECT @ai := @ai+1 AS ac FROM ( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 ) ac1, ( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 ) ac2, ( SELECT @ai := 0 ) xc0 ) ad
获取三十天前的日期
SELECT DATE_SUB(CURDATE(), INTERVAL 30 DAY); -- 获取30天前的数据
获取上月初,上月末的时间
SELECT DATE_FORMAT( DATE_SUB(CURDATE(), INTERVAL 1 MONTH), '%Y-%m-01 00:00:00') AS '上月初';SELECT DATE_FORMAT( LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)), '%Y-%m-%d 23:59:59') AS '上月末';
SELECT DATE_FORMAT(DATE_ADD(DATE_ADD(CURDATE(),INTERVAL -month(CURDATE())+1 month), INTERVAL t.ac-1 month), '%m') AS endTime FROM ( SELECT t.* FROM (SELECT @ai:=@ai+1 AS ac FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3 ) ac1, (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 ) ac2, (SELECT @ai:=0) xc0)t WHERE t.ac <= 12) t
获取本月天数
SELECT t.* FROM (SELECT @ai:=@ai+1 AS ac FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 ) ac1, (SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 ) ac2, (SELECT @ai:=0) xc0)t WHERE t.ac <= (SELECT DATEDIFF(DATE_ADD(CURDATE()-DAY(CURDATE())+1,INTERVAL 1 MONTH ),DATE_ADD(CURDATE(),INTERVAL -DAY(CURDATE())+1 DAY)) AS num FROM DUAL)