当前位置:网站首页>MySQL中根据日期进行范围查询
MySQL中根据日期进行范围查询
2022-08-02 02:57:00 【emgexgb_sef】
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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- VPS8702 VPSC(源特科技)电源管理(PMIC) 封装SOT23-6
- 7-42 整型关键字的散列映射 (25 分)
- 7、MySQL Workbench 导出导入数据库
- analog IC layout
- OperatingSystemMXBean to get system performance metrics
- 01-Node-Express系统框架搭建(express-generator)
- 【无标题】【Koltin Flow(三)】Flow操作符之中间操作符(二)
- ASP WebShell backdoor script and anti-kill
- 【LeetCode】145. Postorder Traversal of Binary Tree
- Hit the programmer interview scene: What did Baidu interviewers ask me?
猜你喜欢
随机推荐
MySQL8.0.28 installation tutorial
node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module ‘./scope‘
Go语学习笔记 - gorm使用 - gorm处理错误 Web框架Gin(十)
MySQL函数(经典收藏)
ASP WebShell 后门脚本与免杀
"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
【LeetCode】145.二叉树的后序遍历
Chapter 10_Index Optimization and Query Optimization
mysql8.0.28 download and installation detailed tutorial, suitable for win11
JS中获取对象数据类型的键值对的键与值
第一章——线性表(顺序表和链表)
PHP WebShell Free Kill
IPFS部署及文件上传(golang)
PHP WebShell 免杀
AcWing 1285. Word Problem Solving (AC Automata)
Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)
Nacos源码分析专题(二)-服务注册
总体写作原则
给你一个大厂面试的机会,你能面试上吗?进来看看!
指针数组和数组指针









