当前位置:网站首页>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
边栏推荐
- E - Red and Blue Graph(组合数学)
- AD单片机九齐单片机NY8B062D SOP16九齐
- The little thing about Request reuse.The research is understood, and I will report it to you.
- DaemonSet of kubernetes and rolling update
- Qt实战案例(55)——利用QDir删除选定文件目录下的空文件夹
- 荣信文化通过注册:年营收3.8亿 王艺桦夫妇为实控人
- 细读《阿里测试之道》
- 可观测性就是对“监控”的包装?
- SQL查询数据以及排序
- 长江欧拉生态创新中心成立,武汉数字经济再添坚实底座
猜你喜欢

立新能源深交所上市:市值55亿 哈密国投与国有基金是股东

Gradle series - Gradle tests, Gradle life cycle, settings.gradle description, Gradle tasks (based on Groovy documentation 4.0.4) day2-3

荣信文化通过注册:年营收3.8亿 王艺桦夫妇为实控人

Gradle系列——Gradle测试,Gradle生命周期,settings.gradle说明,Gradle任务(基于Groovy文档4.0.4)day2-3

SQL每日一练(牛客新题库)——第2天: 条件查询

MCU开发是什么?国内MCU产业现状如何

mysql的基本使用

【无标题】

【码蹄集新手村600题】判断一个数字是否为完全平方数

轮询和长轮询的区别
随机推荐
从零开始Blazor Server(4)--登录系统
考研大事件!这6件事考研人必须知道!
D - I Hate Non-integer Number(背包dp)
搭建ntp时间服务器(安装sql2000配置服务器失败)
mysql查询两个字段值相同的记录
win10+Qt5.15.2 realizes low-power bluetooth control
The little thing about Request reuse.The research is understood, and I will report it to you.
PAT1166 Summit(25)
性能测试入门指南
台积电认清了形势,新的建厂计划没有美国,中国芯片也得到重视
tkinter-TinUI-xml实战(6)问卷
微服务系统架构的演变
openEuler 社区完成首批顾问专家聘用,共同为社区的发展贡献力量
使用ffmpeg来查看视频的信息,fps,和width,height
分布式中的远程调用
灵魂发问:MySQL是如何解决幻读的?
代理商替代义隆153 Aip4210
【每日一题】952. 按公因数计算最大组件大小
什么是混合元编程
DaemonSet of kubernetes and rolling update