- 查询当前月到之前3个月的数据
SELECT * FROM 表名 WHERE period_diff(date_format(now(), '%y%m'), date_format(字段名,'%y%m'))=3;
SELECT * FROM 表名 WHERE 字段名
BETWEEN date_format(
DATE_ADD( DATE_ADD(now(), INTERVAL - DAY (now()) + 1 DAY), INTERVAL - 3 MONTH),
'%Y-%m-%d 00:00:00')
AND date_format(
DATE_ADD(DATE_ADD(now(), INTERVAL - DAY (now()) + 1 DAY), INTERVAL 0 MONTH),
'%Y-%m-%d 00:00:00');
- 近7天的数据
SELECT * FROM 表名 WHERE DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(字段名);
- 昨天一天的数据
SELECT * FROM 表名 WHERE TO_DAYS(NOW()) - TO_DAYS(字段名) = 1
- 查询当前这周的数据
SELECT * FROM 表名 WHERE yearweek(date_format(字段名, '%y-%m-%d')) = yearweek(now());
SELECT * FROM 表名 WHERE yearweek(date_format(字段名,'%y-%m-%d')) = yearweek(now())-1;
- 查询本季度数据
SELECT * FROM 表名 WHERE QUARTER (字段名) = QUARTER (now());
SELECT * FROM 表名 WHERE QUARTER (字段名) = QUARTER (date_sub(now(), INTERVAL 1 QUARTER));