Mysql日期查询的详细介绍
网络编程 2021-07-05 14:37www.168986.cn编程入门
这篇文章主要介绍了Mysql日期查询的详细介绍,根据时间段进行查询数据库中单表中的数据,如果感兴趣的来了解一下
查询当前日期
SELECT CURRENT_DATE(); SELECT CURDATE();
查询当前日期和时间
SELECT NOW();
查询今天的数据
SELECT FROM `表名` WHERE TO_DAYS(NOW()) = TO_DAYS(`字段`); SELECT FROM `表名` WHERE TO_DAYS(NOW()) - TO_DAYS(`字段`) = 0;
查询昨天的数据
SELECT FROM ``表名`` WHERE TO_DAYS(`字段`) = TO_DAYS(NOW()) -1; SELECT FROM ``表名`` WHERE TO_DAYS(`字段`) - TO_DAYS(NOW()) = -1; SELECT FROM ``表名`` WHERE TO_DAYS(NOW()) = TO_DAYS(`字段`) +1; SELECT FROM ``表名`` WHERE TO_DAYS(NOW()) - TO_DAYS(`字段`) = 1;
查询最近七天的数据
SELECT FROM `表名` WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= DATE(`字段`);
查询最近三十天的数据
SELECT FROM table WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= DATE(`字段`);
查询本周的数据
SELECT FROM `表名` WHERE YEARWEEK(date_format(`字段`,'%Y-%m-%d')) = YEARWEEK(CURDATE()); SELECT FROM `表名` WHERE YEARWEEK(date_format(`字段`,'%Y-%m-%d')) - YEARWEEK(CURDATE()) = 0; SELECT FROM `表名` WHERE YEARWEEK(DATE_FORMAT(`字段`,'%Y-%m-%d')) = YEARWEEK(DATE_FORMAT(CURDATE(),'%Y-%m-%d')); SELECT FROM `表名` WHERE YEARWEEK(DATE_FORMAT(`字段`,'%Y-%m-%d')) - YEARWEEK(DATE_FORMAT(CURDATE(),'%Y-%m-%d')) = 0;
查询上周的数据
SELECT FROM `表名` WHERE YEARWEEK(DATE_FORMAT(`字段`,'%Y-%m-%d')) = YEARWEEK(DATE_FORMAT(CURDATE(),'%Y-%m-%d')) - 1; SELECT FROM `表名` WHERE YEARWEEK(DATE_FORMAT(`字段`,'%Y-%m-%d')) - YEARWEEK(DATE_FORMAT(CURDATE(),'%Y-%m-%d')) = -1; SELECT FROM `表名` WHERE YEARWEEK(DATE_FORMAT(`字段`,'%Y-%m-%d')) = YEARWEEK(CURDATE()) - 1; SELECT FROM `表名` WHERE YEARWEEK(DATE_FORMAT(`字段`,'%Y-%m-%d')) - YEARWEEK(CURDATE()) = -1;
查询当月的数据
SELECT FROM `表名` WHERE DATE_FORMAT(`字段`,'%Y-%m') = DATE_FORMAT(CURDATE(),'%Y-%m'); SELECT FROM `表名` WHERE MONTH(`字段`) - MONTH(NOW()) = 0; SELECT FROM `表名` WHERE MONTH(`字段`) = MONTH(NOW());
查询上月的数据
SELECT FROM `表名` WHERE DATE_FORMAT(`字段`,'%Y-%m') = DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 MONTH),'%Y-%m');
查询本季度的数据
SELECT FROM `表名` WHERE QUARTER(`字段`) = QUARTER(NOW()) AND YEAR(`字段`) = YEAR(NOW());
查询上季度的数据
SELECT FROM `表名` WHERE QUARTER(`字段`) = QUARTER(DATE_SUB(NOW(),INTERVAL 1 QUARTER)) AND YEAR(`字段`) = YEAR(NOW());
查询上半年(六个月)的数据
SELECT FROM `表名` WHERE `字段` BETWEEN DATE_SUB(NOW(),INTERVAL 6 MONTH) AND NOW();
查询今年的数据
SELECT FROM `表名` WHERE DATE_FORMAT(`字段`,'%Y') = DATE_FORMAT(NOW(),'%Y'); SELECT FROM `表名` WHERE DATE_FORMAT(`字段`,'%Y') - DATE_FORMAT(NOW(),'%Y') = 0; SELECT FROM `表名` WHERE YEAR(NOW()) = YEAR(`字段`); SELECT FROM `表名` WHERE YEAR(NOW()) - YEAR(`字段`) =0;
查询去年的数据
SELECT FROM `表名` WHERE DATE_FORMAT(`字段`,'%Y') - DATE_FORMAT(NOW(),'%Y') = -1; SELECT FROM `表名` WHERE YEAR(NOW()) = YEAR(`字段`) + 1; SELECT FROM `表名` WHERE YEAR(NOW()) - YEAR(`字段`) = 1;
到此这篇关于Mysql之日期查询的详细介绍的文章就介绍到这了,更多相关Mysql之日期查询内容请搜索狼蚁SEO以前的文章或继续浏览狼蚁网站SEO优化的相关文章希望大家以后多多支持狼蚁SEO!
编程语言
- 甘肃哪有关键词排名优化购买方式有哪些
- 甘肃SEO如何做网站优化
- 河南seo关键词优化怎么做电话营销
- 北京SEO优化如何做QQ群营销
- 来宾百度关键词排名:提升您网站曝光率的关键
- 卢龙关键词优化:提升您网站排名的策略与技巧
- 山东网站优化的注意事项有哪些
- 四川整站优化怎样提升在搜索引擎中的排名
- 疏附整站优化:提升网站性能与用户体验的全新
- 海南seo主要做什么工作售后服务要做到哪些
- 荣昌百度网站优化:提升您网站的搜索引擎排名
- 河北seo网站排名关键词优化如何做SEO
- 江西优化关键词排名推广售后保障一般有哪些
- 古浪SEO优化:提升你的网站可见性
- 西藏网站排名优化怎么把网站排名在百度首页
- 如何提升阳东百度快照排名:详尽指南