码迷,mamicode.com
首页 > 数据库 > 详细

mysql根据日期查询

时间:2014-12-01 06:25:04      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   on   line   sql   mysql   table   br   

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天

mysql根据日期查询

标签:io   ar   for   on   line   sql   mysql   table   br   

原文地址:http://www.cnblogs.com/like2php/p/4134065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!