首页 > 数据库技术 > 详细

mysql根据日期查询

时间:2014-12-01 06:23:11      阅读:261      评论:0      收藏:0      [点我收藏+]

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根据日期查询

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!