第一种:数量小的时候用,数据量稍微起来巨慢
select 字段 from 表名 where to_days(时间字段) = to_days(now());
第二种:速度快
select 字段 from 表名 where 时间字段 BETWEEN CONCAT(CURDATE(),' 00:00:00') AND CONCAT(CURDATE(),' 23:59:59');
最好配合复合索引来查,避免全表扫描
SELECT * FROM `day_sell` WHERE TO_DAYS(NOW()) - TO_DAYS(sell_time) <= 1
#7天的数据
SELECT * FROM `day_sell` WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) < DATE(sell_time);
SELECT * FROM day_sell WHERE YEARWEEK(DATE_FORMAT(sell_time,'%Y-%m-%d')) = YEARWEEK(NOW());
#上周的数据
SELECT * FROM day_sell WHERE YEARWEEK(DATE_FORMAT(sell_time,'%Y-%m-%d')) = YEARWEEK(NOW()) -1;
SELECT * FROM `day_sell` WHERE DATE_FORMAT(sell_time,'%Y-%m')= DATE_FORMAT(CURDATE(),'%Y-%m')
SELECT * FROM day_sell WHERE PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(sell_time,'%Y%m')) = 1;
SELECT * FROM day_sell WHERE QUARTER(sell_time) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER));
SELECT * FROM `day_sell` WHERE QUARTER(sell_time) = QUARTER(NOW())
SELECT * FROM `day_sell` WHERE YEAR(sell_time) = YEAR(NOW())
SELECT DATEDIFF('2020-10-29','2020-10-28') AS days;
SELECT DATE_FORMAT(sell_time,'%Y%u') weeks,COUNT(sell_time) COUNT FROM `day_sell` GROUP BY weeks;
SELECT DATE_FORMAT(sell_time,'%Y%m%d') days,COUNT(sell_time) COUNT FROM day_sell GROUP BY days;
SELECT DATE_FORMAT(sell_time,'%Y%m') months,COUNT(sell_time) COUNT FROM day_sell GROUP BY months;
SELECT * FROM day_sell WHERE YEAR(sell_time) = YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR));
SELECT * FROM day_sell WHERE sell_time BETWEEN DATE_SUB(NOW(),INTERVAL 6 MONTH) AND NOW();