您的位置:首页 > 博客中心 > 数据库 >

mysql根据日期查询

时间:2022-03-14 03:13

select * from table where to_days(dateline) = to_days(now());
select * from table where date(dateline) = curdate();

--查询昨天记录
select * from table where to_days(dateline) = to_days(now())-1;
select * from table where date(dateline) = curdate()-1; --今天是本月的第一天查不到上月最后一天记录

--查询本周记录
select * from table where YEARWEEK(date_format(dateline,‘%Y-%m-%d‘)) = YEARWEEK(now());
select * from table where DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(dateline); --查前7天

--查询上周记录
select * from table where YEARWEEK(date_format(dateline,‘%Y-%m-%d‘)) = YEARWEEK(now())-1;

--查询本月记录
select * from table where DATE_FORMAT(dateline, ‘%Y%m‘) = DATE_FORMAT(CURDATE(),‘%Y%m‘);
select * from table where DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(dateline);--查前30天

--查询上月记录
select * from table where PERIOD_DIFF(date_format( now() ,‘%Y%m‘) , date_format(dateline, ‘%Y%m‘ ) ) =1;
select * from table where DATE_FORMAT(dateline, ‘%Y%m‘) = DATE_FORMAT(CURDATE(),‘%Y%m‘)-1;
select * from table where date_format(dateline,‘%Y-%m‘)=date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),‘%Y-%m‘); --查前60天到前30天

热门排行

今日推荐

热门手游