mysql 统计当天,本周,本月,上一月的数据

今天

select * from `ht_invoice_information` where year(create_date)=year(date_sub(now(),interval 1 year));
select * from 表名 where to_days(时间字段名) = to_days(now());

昨天

select * from 表名 where to_days( now( ) ) - to_days( 时间字段名) <= 1

近期7天

select * from 表名 where date_sub(curdate(), interval 7 day) <= date(时间字段名)

近30天

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 `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));

 

查询当前这周的数据

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now());

查询上周

SELECT name,submittime FROM enterprise WHERE YEARWEEK(date_format(submittime,'%Y-%m-%d')) = YEARWEEK(now())-1;

 

你可能感兴趣的:(mysql 统计当天,本周,本月,上一月的数据)