当前位置:网站首页>Solve the problems arising from the mysql statement MAX () function
Solve the problems arising from the mysql statement MAX () function
2022-08-06 00:29:00 【YHSevenWater】
In the database, when some of our data is not yet clear about the specific type, it is not a problem to use the varchar type to represent the field type, (this is a good habit!) Because the varchar type data stores numeric types or charactersString types can be handled, and it is relatively simple to perform switching calculations in the back-end code, just do a little processing.
And this blog post aims to record the problems that occurred in the MAX() function in the mysql statement of the project. Although the problem is small, it is quite a headache if you don't know where the problem is. After all, I haveIt was very confusing before.
is shown below, the current_data type is varchar when designing the table structure: 
According to the idiomatic way of writing in our mysql statement, the MAX() and MIN() functions should be written as follows:
SELECTMAX(current_data) AS maxCurrentData,MIN(current_data) AS minCurrentDataFROM tunnel_detect_dataWHERE create_time >= '2022-06-01'AND '2022-06-02' >= create_time The result of the query is as follows:
The query result is compared with the actual data in the table below, and it is found that the result is incorrect. In the comparison, several data are greater than the value of 993.07. Why does this happen?
Reason: the data is varchar type, then the MAX function getsThe maximum value will be compared from the beginning, that is to say, the maximum value obtained by the MAX function are all values starting with 9, and the number with more than 9 at the beginning is considered to be the maximum value, which leads to deviations in the results we query.
Solution: If the data type in the table is not a varchar type but a specific numerical type, there is no problem with the above sql statement, but if it is a varchar type of data, the query field data must be "+0" processing, otherwise it does not match the actual result, as shown below:
SELECTMAX(current_data + 0) AS maxCurrentData,MIN(current_data + 0) AS minCurrentDataFROM tunnel_detect_dataWHERE create_time >= '2022-06-01'AND '2022-06-02' >= create_time The query result is as follows:
The actual comparison of the data in the following table shows that the query result after the "+0" operation is the correct result.
To summarize: Judge the mysql statement MAX() function China needs to do "+0" processing, specific problems are analyzed in detail, bugs are solved, a lot of fun!
Passing friends, if the blog post helps you solve your problem, you can like + bookmark + follow a wave~ I will continue to update blog posts related to project learning, thank you for your support!!!Wuhu take off ️️️
边栏推荐
- 狄兰·托马斯诗合集▷Do not go gentle into that good night
- oracle学习笔记识别低效sql(十九)
- [PMP Study Notes] Chapter 4 Project Integration Management
- STM32——USMART调试组件实验-M4
- 正则表达式完整入门教程,含在线练习
- HBuilderX编译的报错提示:pages.json tabBar[‘list‘][0][‘pagePath‘] “/pages/index/index“ 需在 pages 数组中
- Qt入门(三)——计算器项目的实现
- 物理 NFT 有什么特别之处?
- OA项目线程内共享上下文实例(二)
- Is it safe to choose a mobile phone application for a securities account?
猜你喜欢

Yiwei Lithium's first system product equipped with 46 series large cylindrical batteries successfully rolled off the production line
![[统计机器学习] 线性分类模型](/img/1b/5f989e4180f5751d3bff04ee5e0465.png)
[统计机器学习] 线性分类模型

Kubernetes构建Redis 集群

Vim编辑器常用命令汇总

物理 NFT 有什么特别之处?

为啥强烈禁止使用Calendar?

openstack架构中给虚拟机提供存储资源的组件是什么

正则表达式完整入门教程,含在线练习

How to fix wsa cannot be installed

工作中常见的两种谬误
随机推荐
【瑞吉外卖】day01:整体介绍以及开发环境搭建
物理 NFT 有什么特别之处?
Playing air guitar in VR?Developer: Gesture optimization has skills
2022第四届山东国际大健康产业博览会,中国大健康产业展览会
iNFTnews | 元宇宙将如何改变教育?
[2022 强网杯] house_of_cat 战战兢兢的复现
Qt入门(三)——计算器项目的实现
Day3:面试必考选择题
jsp学习
Chrome Google Chrome, how to install the appropriate browser driver?
在国信证券股票开户怎么样?安全吗?
oracle学习笔记识别低效sql(十九)
Kubernetes 调度器优化
【论文阅读】Virtual Adversarial Training: a Regularization Method for SL and SSL
技术术语积累
阿里资深技术专家带你聊一聊服务端的接口测试
云上的移动性能测试平台
【u-boot】如何自定义u-boot的命令
安全测试-AndroidManifest.xml元素分析(1)
简单了解 JWT