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

MySQL关于日期的查询sql

时间:2016-09-27 20:55:49      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:mysql 日期查询

---查询今天的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE  TO_DAYS(create_date) = TO_DAYS(NOW());

SELECT * FROM wmacc.`cash_trade_record` WHERE  DATE_FORMAT(create_date, ‘%Y%m%d‘) = DATE_FORMAT(CURDATE(), ‘%Y%m%d‘);

---查询昨天的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE  TO_DAYS(create_date) = TO_DAYS(NOW()) - 1;

---查询最近7天的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE DATE_SUB(NOW(), INTERVAL 7 DAY) <= create_date;

---查询本周的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE YEARWEEK(DATE_FORMAT(create_date,‘%Y%m%d‘)) = YEARWEEK(NOW());

---查询上周的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE YEARWEEK(DATE_FORMAT(create_date,‘%Y%m%d‘)) = YEARWEEK(NOW()) - 1;

---查询本月的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE DATE_FORMAT(create_date, ‘%Y%m‘) = DATE_FORMAT(CURDATE(), ‘%Y%m‘);

---查询上个月的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE PERIOD_DIFF(DATE_FORMAT(NOW(), ‘%Y%m‘),DATE_FORMAT(create_date, ‘%Y%m‘))=1;

SELECT * FROM wmacc.`cash_trade_record` WHERE DATE_FORMAT(create_date, ‘%Y%m‘) = DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 MONTH), ‘%Y%m‘);

---查询本季度的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE QUARTER(create_date) = QUARTER(CURDATE());

---查询上季度的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE QUARTER(create_date) = QUARTER(DATE_SUB(NOW(), INTERVAL 1 QUARTER));

---查询本年的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE YEAR(create_date) = YEAR(NOW());

---查询上一年的记录

SELECT * FROM wmacc.`cash_trade_record` WHERE YEAR(create_date)=YEAR(DATE_SUB(NOW(),INTERVAL 1 YEAR));



本文出自 “Flyfish” 博客,请务必保留此出处http://9381188.blog.51cto.com/9371188/1856961

MySQL关于日期的查询sql

标签:mysql 日期查询

原文地址:http://9381188.blog.51cto.com/9371188/1856961

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