今天花了些时间整理了下MySQL中分别查找当天、昨天、近一周、近一个月等等时间段数据的代码
1、查询今天数据的语句
select * frim 表名 where to_days(时间字段名)=to_days(now());
select now();//获得当前时间 格式:2016-01-12 23:21:33
select curdate();//获得当前时间 格式: 2016-01-12
select curtime();//获得当前时间 格式:23:19:43
2、查询昨天数据的语句
select * from 表名 where to_days(now())-to_days(时间字段名)=1;
3、查询七天语句
select * from 表名 where date_sub(curdate(),interval 7 day)<=date(时间字段名);
或者 select * from 表名 where 时间字段名>=date_sub(curdate(),interval 7 day);
4、查询近一个月的语句
select * from 表名 where date_sub(curdate(),interval 1 month)<=date(时间字段名);
或者 select * from 表名 where 时间字段>=date_sub(curdate(),interval 1 month);
5、查询本季度数据
select * from 表名 where QUARTER(时间字段名)=QUARTER(now());
QUARTER(date);//返回的一年日期,取值范围为1至4季度。
6、查询上季度数据
select * from 表