当前位置:网站首页>MySQL中根据日期进行范围查询
MySQL中根据日期进行范围查询
2022-08-01 14:22:00 【程序员超时空】
1.MySQL中查询某年某月的数据
测试数据如下;
1.1查询2018年的数据:
select * from day_rate where year(date)='2018'
1.2查询2月份的数据:
select * from day_rate where month(date)='02'
1.3查询2019年2月份的数据:
select * from day_rate where year(date)='2019' and month(date)='02'
1.4查询年初第32天的数据:
select * from day_rate where dayofyear(date)='32'
2.日期进行范围查询时不能超出某月的最大日期
好像是mysql8,使用between…and…查询时,当查询的右边界为2021-2-31时,超出了实际范围(因为2月不可能有31天),所以无法进行查询。
例如:
select * from t_ordersetting where orderDate between '2021-09-01' and '2021-09-31';
这个方法不行,因为9月没有31号,超出了9月的最大日期,所以查询错误。
解决方法:
方法一(已经过验证,可行):
使用 select * from t_ordersetting where year(orderDate)=#{year} and month(orderDate)=#{month};
查询某年某月的数据,如下:
<select id="getOrderSettingByMonth" parameterType="map" resultType="com.itheima.pojo.OrderSetting">
select * from t_ordersetting where year(orderDate)=#{year} and month(orderDate)=#{month};
</select>
方法二(查看评论得知,没验证,不知是否可行):
使用:SELECT * FROM t_ordersetting WHERE orderDate BETWEEN STR_TO_DATE('2021-04-1','%Y-%m-%d') AND STR_TO_DATE('2021-04-31','%Y-%m-%d')
MYsql 8数据库select * from t_ordersetting where orderDate between ‘2020-2-1‘ and ‘2020-2-31‘语句竟然不能用
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- The little thing about Request reuse.The research is understood, and I will report it to you.
- 解读selenium webdriver
- The problem that the column becomes indexed after pd groupby and the aggregation column has no column name
- Chat technology in live broadcast system (8): Architecture practice of IM message module in vivo live broadcast system
- 分布式中的CAP原理
- [LiteratureReview]Optimal and Robust Category-level Perception: Object Pose and Shape Estimation f
- WPF如何自定义隐藏下拉框选项
- The role of the final keyword final and basic types, reference types
- PIR人体感应AC系列感应器投光灯人体感应开关等应用定制方案
- 魔众文档管理系统 v5.0.0
猜你喜欢
免费使用高性能的GPU和TPU—谷歌Colab使用教程
灵魂发问:MySQL是如何解决幻读的?
使用open3d可视化3d人脸
HTB-Mirai
1161. 最大层内元素和
Gradle series - Gradle tests, Gradle life cycle, settings.gradle description, Gradle tasks (based on Groovy documentation 4.0.4) day2-3
软件测试之发现和解决bug
Wovent Bio IPO: Annual revenue of 480 million pension fund is a shareholder
openEuler 社区完成首批顾问专家聘用,共同为社区的发展贡献力量
微信UI在线聊天源码 聊天系统PHP采用 PHP 编写的聊天软件,简直就是一个完整的迷你版微信
随机推荐
利用UIRecorder做页面元素巡检
What is a closure?
沃文特生物IPO过会:年营收4.8亿 养老基金是股东
测试如何拓展自己的知识面?
【论文笔记】MiniSeg: An Extremely Minimum Network for Efficient COVID-19 Segmentation
xmind2testcase:高效的测试用例导出工具
Service Mesher Meetup 成都站:Service Mesh是下一代SDN吗?
透过现象看本质,如何针对用户做好需求分析
E - Red and Blue Graph (Combinatorics)
Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
表白代码vbs不同意无法关闭(vbs表白代码不同意按键会跑)
final关键字的作用 final和基本类型、引用类型
【无标题】
【每日一题】1331. 数组序号转换
MCU开发是什么?国内MCU产业现状如何
有限合伙人与普通合伙人的区别
Yann LeCun开怼谷歌研究:目标传播早就有了,你们创新在哪里?
The role of the final keyword final and basic types, reference types
性能优化——资源优化笔记
ThreadLocal保存用户登录信息