mysql查詢今天、昨天、7天、近30天、本月、上一月 數據mysql
#今天sql
select * from 表名 where to_days(時間字段名) = to_days(now());
#昨天code
SELECT * FROM 表名 WHERE TO_DAYS( NOW( ) ) - TO_DAYS( 時間字段名) = 1
#7天orm
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(時間字段名)
#近30天io
SELECT * FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(時間字段名)
#本月form
SELECT * FROM 表名 WHERE DATE_FORMAT( 時間字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )
#上一月date
SELECT * FROM 表名 WHERE PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format( 時間字段名, '%Y%m' ) ) =1
#查詢本季度數據select
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(now());
#查詢上季度數據數據
select * from `ht_invoice_information` where QUARTER(create_date)=QUARTER(DATE_SUB(now(),interval 1 QUARTER));
#查詢本年數據查詢
select * from `ht_invoice_information` where YEAR(create_date)=YEAR(NOW());
#查詢上年數據
select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));