MySql查询最近一个月,一周,一天

时间:2023-12-23 08:19:50

最近一个月

SELECT * FROM table WHERE DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(time);   

本月、当前月

SELECT * FROM table WHERE DATE_FORMAT(o.createTime,'%Y-%m') = DATE_FORMAT(CURDATE(),'%Y-%m');   

上个月

SELECT * FROM table WHERE PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y-%M'),DATE_FORMAT(create_time,'%Y-%m')) = 1;  

最近一周、7天

SELECT * FROM table WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(time);   

最近一天

SELECT * FROM table WHERE DATE_SUB(CURDATE(), INTERVAL 1 DAY) <= date(time);   

当天

SELECT * FROM table WHERE to_days(create_time) = to_days(now());