1、查询今日数据
select * from 表名 where to_days(时间字段名) = to_days(now());
//速度快
select id from hz_order where create_time BETWEEN CONCAT(CURDATE(),' 00:00:00') AND CONCAT(CURDATE(),' 23:59:59');
2、查询昨日数据
select * from 表名 where to_days(now()) - to_days(时间字段名) <= 1;
3、查询近7天数据
select * from 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(时间字段名);
4、查询近30天数据
select * from 表名 where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(时间字段名);
5、查询本月数据
select * from 表名 where DATE_FORMAT(时间字段名, '%Y%m') = DATE_FORMAT(CURDATE( ), '%Y%m');
6、查询上个月数据
select * from 表名 where PERIOD_DIFF(DATE_FORMAT(now() , '%Y%m'), DATE_FORMAT(时间字段名, '%Y%m')) = 1;
7、查询本季度数据
select * from 表名 where QUARTER(时间字段) = QUARTER(now());
8、查询上季度数据
select * from 表名 where QUARTER(时间字段) = QUARTER(DATE_SUB(now(),interval 1 QUARTER));
9、查询本年数据
select * from 表名 where YEAR(时间字段) = YEAR(now());
10、查询上年数据
select * from 表名 where YEAR(时间字段) = YEAR(DATE_SUB(now(), INTERVAL 1 year));
11、查询当前这周数据
select * from 表名 where YEARWEEK(date_format(时间字段,'%Y-%m-%d')) = YEARWEEK(now());
12、查询上周数据
select * from 表名 where YEARWEEK(date_format(时间字段,'%Y-%m-%d')) = YEARWEEK(now())-1;
13、查询上个月的数据
select * from 表名 where date_format(submittime,'%Y-%m') = date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m')
select * from 表名 where DATE_FORMAT(时间字段,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m');
select * from 表名 where WEEKOFYEAR(FROM_UNIXTIME(时间字段,'%y-%m-%d')) = WEEKOFYEAR(now());
select * from 表名 where MONTH(FROM_UNIXTIME(时间字段,'%y-%m-%d')) = MONTH(now());
select * from 表名 where YEAR(FROM_UNIXTIME(时间字段,'%y-%m-%d')) = YEAR(now()) and MONTH(FROM_UNIXTIME(时间字段,'%y-%m-%d')) = MONTH(now());
select * from 表名 where 时间字段 between 上月最后一天 and 下月第一天;
14、查询当前月份的数据
select * from 表名 where DATE_FORMAT(时间字段,'%Y-%m') = DATE_FORMAT(now(),'%Y-%m');
15、查询距离现在6个月内的数据
select * from 表名 where 时间字段 between DATE_SUB(now(), INTERVAL 6 MONTH) and now();
16、查询10分钟以内的数据
select * from 表名 where 时间字段 >= CURRENT_TIMESTAMP - INTERVAL 10 MINUTE;
17、判断日期字段是否在当前时间前后10分钟内
select * from jw_kq_dkgz
where
(unix_timestamp(start_time) between unix_timestamp(CURRENT_TIMESTAMP - INTERVAL 10 MINUTE) and unix_timestamp(CURRENT_TIMESTAMP + INTERVAL 10 MINUTE) )
or
(unix_timestamp(end_time) between unix_timestamp(CURRENT_TIMESTAMP - INTERVAL 10 MINUTE) and unix_timestamp(CURRENT_TIMESTAMP + INTERVAL 10 MINUTE) )