当前位置:网站首页>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
边栏推荐
- 你真的会测试用户登录吗?
- Amperon IPO meeting: annual revenue of 500 million Tongchuang Weiye and China Mobile Innovation are shareholders
- 台积电认清了形势,新的建厂计划没有美国,中国芯片也得到重视
- 反序列化漏洞详解
- 利用UIRecorder做页面元素巡检
- AtCoder Beginner Contest 261 D - Flipping and Bonus
- 性能优化——动画优化笔记
- 【码蹄集新手村600题】判断一个数字是否为完全平方数
- 2022-07-29 网工进阶(二十二)BGP-其他特性(路由过滤、团体属性、认证、AS欺骗、对等体组、子路由器、路由最大接收数量)
- 性能测试入门指南
猜你喜欢

mysql查询两个字段值相同的记录
![[LiteratureReview]Optimal and Robust Category-level Perception: Object Pose and Shape Estimation f](/img/bc/f3cea50c157f151a1ca5e540e7f77b.png)
[LiteratureReview]Optimal and Robust Category-level Perception: Object Pose and Shape Estimation f

ThreadLocal保存用户登录信息

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

响应式2022英文企业官网源码,感觉挺有创意的

The role of the final keyword final and basic types, reference types

性能测试入门指南

经纬信息IPO过会:年营收3.5亿 叶肖华控制46.3%股权

mysql的基本使用

PAT 1167 Cartesian Tree(30)
随机推荐
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
牛客刷SQL--6
lua脚本关键
COPU 陆首群教授在 openEuler 社区首批高级顾问聘任仪式上发言
qt 通用ui
我寻找的方向
性能优化——资源优化笔记
WPF如何自定义隐藏下拉框选项
什么是混合元编程
【每日一题】1161. 最大层内元素和
math.pow()函数用法[通俗易懂]
JSON数据转换总结(VIP典藏版)
The role of the final keyword final and basic types, reference types
视频传输协议(常用的视频协议)
微服务原生案例搭建
灵魂发问:MySQL是如何解决幻读的?
OpenSSL SSL_read: Connection was reset, errno 10054
Gradle系列——Gradle测试,Gradle生命周期,settings.gradle说明,Gradle任务(基于Groovy文档4.0.4)day2-3
立新能源深交所上市:市值55亿 哈密国投与国有基金是股东
MySQL中的行锁