当前位置:网站首页>Range query based on date in MySQL
Range query based on date in MySQL
2022-08-01 14:31:00 【Programmer over time】
1. Query the data of a certain year and a certain month in MySQL
The test data is as follows;
1.1 Query 2018 data:
select * from day_rate where year(date)='2018'
1.2 Query data for February:
select * from day_rate where month(date)='02'
1.3 Query data for February 2019:
select * from day_rate where year(date)='2019' and month(date)='02'
1.4 Query the data on the 32nd day of the beginning of the year:
select * from day_rate where dayofyear(date)='32'
Query the data of a certain year and a certain month in MySQL
2. The date range query cannot exceed the maximum date of a month
It seems to be mysql8. When using between...and... query, when the right boundary of the query is 2021-2-31, it exceeds the actual range (because there can be no 31 days in February), so the query cannot be performed.
Example:
select * from t_ordersetting where orderDate between '2021-09-01' and '2021-09-31';
This method does not work, because there is no 31st in September, which exceeds the maximum date of September, so the query is wrong.
Solution:
Method 1 (verified and feasible):
Use select * from t_ordersetting where year(orderDate)=#{year} and month(orderDate)=#{month};
Query the data of a certain month and a certain year, as follows:
Method 2 (check the comments, it is not verified, I don't know if it is feasible):
Use: 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')
Let me introduce myself first. The editor graduated from Shanghai Jiaotong University in 2013. I worked in a small company and went to big factories such as Huawei and OPPO. I joined Alibaba in 2018, until now.I know that most junior and intermediate java engineers want to upgrade their skills, they often need to explore their own growth or sign up to study, but for training institutions, the tuition fee is nearly 10,000 yuan, which is really stressful.Self-learning that is not systematic is very inefficient and lengthy, and it is easy to hit the ceiling and the technology stops.Therefore, I collected a "full set of learning materials for java development" for everyone. The original intention is also very simple. I hope to help friends who want to learn by themselves but don't know where to start, and at the same time reduce everyone's burden.Add the business card below to get a full set of learning materials
边栏推荐
猜你喜欢
轮询和长轮询的区别
Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
微服务系统架构的演变
MySQL中根据日期进行范围查询
The role of the final keyword final and basic types, reference types
使用open3d可视化3d人脸
只知道SQL数据库?又一国产数据库语言诞生了
倪光南:openEuler已达国际同类社区水准
Gradle series - Gradle tests, Gradle life cycle, settings.gradle description, Gradle tasks (based on Groovy documentation 4.0.4) day2-3
有限合伙人与普通合伙人的区别
随机推荐
Pytorch —— 分布式模型训练
win10+Qt5.15.2实现低功耗蓝牙控制
[机缘参悟-57]:《素书》-4-修身养志[本德宗道章第四]
Chat technology in live broadcast system (8): Architecture practice of IM message module in vivo live broadcast system
表白代码vbs不同意无法关闭(vbs表白代码不同意按键会跑)
长江欧拉生态创新中心成立,武汉数字经济再添坚实底座
如何使用 Mashup 技术在 SAP Cloud for Customer 页面嵌入自定义 UI
台积电认清了形势,新的建厂计划没有美国,中国芯片也得到重视
动态模型中嵌入静态模型实践
2022年5月20日最全摸鱼游戏导航
Typora报错:This beta version of Typora is expired
视频传输协议(常用的视频协议)
openEuler 社区12位开发者荣获年度开源贡献之星
CSDN配置功能总结
关于Request复用的那点破事儿。研究明白了,给你汇报一下。
微服务系统架构的演变
分布式中的CAP原理
牛客刷SQL--3
kubernetes之DaemonSet以及滚动更新
170页6万字智慧能源管理平台建设方案书