mysql 查询当天、本周,本月,上一个月的数据
select * from 表名 where to_days ( 时间字段名) = to_days ( now ( ) ) ;
SELECT * FROM 表名 WHERE TO_DAYS ( NOW ( ) ) - TO_DAYS ( 时间字段名) <= 1
SELECT * FROM 表名 where DATE_SUB ( CURDATE ( ) , INTERVAL 7 DAY) <= date ( 时间字段名)
SELECT * FROM 表名 WHERE YEARWEEK ( date_format ( 时间字段名, '%Y-%m-%d' ) ) = YEARWEEK ( now ( ) ) ;
SELECT * FROM 表名 WHERE YEARWEEK ( date_format ( 时间字段名, '%Y-%m-%d' ) ) = YEARWEEK ( now ( ) ) - 1 ;
SELECT * FROM 表名 where DATE_SUB ( CURDATE ( ) , INTERVAL 30 DAY) <= date ( 时间字段名)
SELECT * FROM 表名 WHERE DATE_FORMAT ( 时间字段名, '%Y%m' ) = DATE_FORMAT ( CURDATE ( ) , '%Y%m' )
SELECT * FROM 表名 WHERE PERIOD_DIFF ( date_format ( now ( ) , '%Y%m' ) , date_format ( 时间字段名, '%Y%m' ) ) = 1
select * from 表名 where QUARTER ( 时间字段名) = QUARTER ( now ( ) ) ;
select * from 表名 where QUARTER ( 时间字段名) = QUARTER ( DATE_SUB ( now ( ) , interval 1 QUARTER) ) ;
select * from 表名 where 时间字段名 between date_sub ( now ( ) , interval 6 month) and now ( ) ;
select * from 表名 where YEAR ( 时间字段名) = YEAR ( NOW ( ) ) ;
select * from 表名 where year ( 时间字段名) = year ( date_sub ( now ( ) , interval 1 year) ) ;