当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢

How ReentrantLock works

I will give you a chance to interview in a big factory. Can you interview?Come in and see!

Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)

Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL Web框架Gin(九)

MySQL8.0.28 installation tutorial

Chapter 11_Database Design Specifications

mockjs生成假数据的基本使用

Webshell上传方式

PHP WebSehll 后门脚本与检测工具

Common SQL interview questions: 50 classic examples
随机推荐
就瞎写=感想
【LeetCode】144.二叉树的前序遍历
【每日一道LeetCode】——9. 回文数
MySQL8--Windows下使用msi(图形界面)安装的方法
架构:应用架构的演进以及微服务架构的落地实践
Hit the programmer interview scene: What did Baidu interviewers ask me?
7-43 字符串关键字的散列映射 (25 分) 谜之测试点
知识体系树
【LeetCode】102. Level order traversal of binary tree
7-42 整型关键字的散列映射 (25 分)
Go简单实现协程池
pyqt上手体验
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL Web框架Gin(九)
analog IC layout-Design for reliability
[Daily LeetCode]——1. The sum of two numbers
mysql8.0.28下载和安装详细教程,适配win11
【LeetCode】145.二叉树的后序遍历
aws s3上传文件
PyTorch(六)——PyTorch可视化
MySQL8 -- use msi (graphical user interface) under Windows installation method