查询当天数据
SELECT * FROM 表名 WHERE TO_DAYS(时间字段名) = TO_DAYS(NOW());
查询昨天数据
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;
查询距离当前现在6个月的数据
SELECT * FROM 表名 WHERE 时间字段名 BETWEEN DATE_SUB(NOW(), INTERVAL 6 MONTH) AND NOW();
查询近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 表名 WHERE QUARTER (时间字段名) = QUARTER (NOW());
查询上季度数据
SELECT * FROM 表名 WHERE QUARTER (时间字段名) = QUARTER ( DATE_SUB(NOW(), INTERVAL 1 QUARTER));
查询本年数据
SELECT * FROM 表名 WHERE YEAR (时间字段名) = YEAR (NOW());
查询上年数据
SELECT * FROM 表名 WHERE YEAR (时间字段名) = YEAR ( date_sub(NOW(), INTERVAL 1 YEAR));