参考:

https://blog.51cto.com/u_16099304/6335011

1、查询当天的数据

select * from 表名 where TO_DAYS(时间字段)=TO_DAYS(NOW());

例如:

select count(1) from movie_item where TO_DAYS(create_time)=TO_DAYS(NOW());

SELECT * FROM your_table_name WHERE DATE(your_date_column) = CURDATE();

例如:

SELECT count(1) FROM movie_item WHERE DATE(create_time) = CURDATE();

2、查询当周的数据

select * from 表名 where YEARWEEK(DATE_FORMAT(时间字段,'%Y-%m-%d'))=YEARWEEK(NOW());

例如:

select count(1) from movie_item where YEARWEEK(DATE_FORMAT(create_time,'%Y-%m-%d'))=YEARWEEK(NOW());

3、查询当月的数据

select * from 表名 where DATE_FORMAT(时间字段,'%Y%m')=DATE_FORMAT(DATE_SUB(curdate(),INTERVAL 1 DAY),'%Y-%m-%d') ;

4、查询昨天的数据

select * from 表名 where DATE_FORMAT(时间字段,'%Y-%m-%d')=DATE_FORMAT(DATE_SUB(curdate(),INTERVAL 1 DAY),'%Y-%m-%d') ;

例如:

select date(create_time) as day, platform, count(1) as count from movie_item where DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(create_time) group by day desc, platform desc

5、查询最近7天的数据

select * from 表名 where DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=DATE(时间字段);

6、查询当年的数据

select * from 表名 where YEAR(时间字段) =YEAR(NOW());

7、查询上周的数据

select * from 表名 whereYEARWEEK(DATE_FORMAT(时间字段,'%Y-%m-%d'))=YEARWEEK(NOW())-1;

8、查询上月的数据

select *from 表名 where 时间字段=DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH),'%Y-%m')

9、查询昨天23:00的数据

SELECT DATE_FORMAT(date_sub('2019-10-16 00',INTERVAL 1 hour),'%Y-%m-%d %H')