当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
什么是闭包?
PAT1166 Summit(25)
D - I Hate Non-integer Number(背包dp)
游戏元宇宙发展趋势展望分析
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
测试如何拓展自己的知识面?
kubernetes之DaemonSet以及滚动更新
redis主从同步方式(redis数据同步原理)
pd groupby后列变索引以及聚合列无列名的问题
论文详读《基于改进 LeNet-5 模型的手写体中文识别》,未完待补充
lua脚本关键
立新能源深交所上市:市值55亿 哈密国投与国有基金是股东
AD单片机九齐单片机NY8B062D SOP16九齐
final关键字的作用 final和基本类型、引用类型
gpio模拟串口通信
D - Draw Your Cards(模拟)
利用UIRecorder做页面元素巡检
Gradle系列——Gradle测试,Gradle生命周期,settings.gradle说明,Gradle任务(基于Groovy文档4.0.4)day2-3
视频传输协议(常用的视频协议)
Wovent Bio IPO: Annual revenue of 480 million pension fund is a shareholder