SELECT sum(1) as years,
(select sum(1) from `表名` where DATE_FORMAT( 时间字段名, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m')) as months,
(select sum(1) from `表名` where to_days(时间字段名) = to_days(now()))as days
FROM `表名` where year(regtime)=year(now())
当天 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(时间字段名)
近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 WEEKOFYEAR(create_date)=WEEKOFYEAR(NOW());
#查询上周记录
select * from ht_invoice_information where create_date>=date_add(now(),interval -(8 + weekday(now())) day) and create_date<=date_add(now(),interval -(1 + weekday(now())) day);
或者
select * from `ht_invoice_information` where WEEKOFYEAR(create_date)=WEEKOFYEAR(DATE_SUB(now(),INTERVAL 1 week));
#查询本月数据
select * from ht_invoice_information where MONTH(create_date)=MONTH(NOW()) and year(create_date)=year(now());
#查询上月数据
select * from ht_invoice_information where create_date<=last_day(date_add(now(),interval -1 MONTH)) and create_date>=DATE_FORMAT(concat(extract(year_month from date_add(now(),interval -1 MONTH)),’01′),’%Y-%m-%d’);
或者
select * from `ht_invoice_information` where MONTH(create_date)=MONTH(DATE_SUB(NOW(),interval 1 month))
and year(create_date)=year(now());
#查询本季度数据
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 YEAR(procurement_dt)*10+((MONTH(procurement_dt)-1) DIV 3) +1, MONTH(procurement_dt) , procurement_dt from xs001
groupbyconcat(date_format(savetime,'%Y'),FLOOR((date_format(savetime,'%m')+2)/3))
按月统计数据
统计2010年 每月的资金select DATE_FORMAT(date,'%Y-%m') as month,sum(money) as money from finance where DATE_FORMAT(date,'%Y')=2010 group by
month order by month
统计每个星期select DATE_FORMAT(date,'%x年-第%v周') as week,sum(money) as money from finance_base where DATE_FORMAT(date,'%Y')=2010 group by
week