最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Mysql日期如何查询 Mysql日期查询代码示例
时间:2022-06-29 08:54:07 编辑:袖梨 来源:一聚教程网
Mysql日期如何查询?本篇文章小编给大家分享一下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;
相关文章
- 江南百景图听风塔怎么样 11-22
- 原神恰斯卡圣遗物怎么搭配 11-22
- 2024年霸王茶姬11月22日口令是什么 2024.11.22霸王茶姬口令介绍 11-22
- 光遇11.21季节蜡烛在哪里 光遇11月21日季节蜡烛位置攻略 11-22
- 光遇11.21大蜡烛在哪里 光遇11月21日大蜡烛位置攻略 11-22
- 光遇11.21免费魔法有什么 光遇11月21日免费魔法收集攻略 11-22