Mysql method to query data within 30 days: execute the sql statement [SELECT * FROM table name where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date (time field name);].
Today:
(Recommended tutorial:mysql tutorial)
SELECT * FROM 表名 WHERE TO_DAYS( 时间字段名) = TO_DAYS(NOW());
Yesterday:
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 时间字段名) <= 1;
7 days ago:
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名);
30 days:
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名);
This month:
SELECT * FROM 表名 WHERE DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' );
Related recommendations:php training
The above is the detailed content of How to query data within 30 days in mysql. For more information, please follow other related articles on the PHP Chinese website!