当前位置:网站首页>MySQL 巨坑:update 更新慎用影响行数做判断!!!
MySQL 巨坑:update 更新慎用影响行数做判断!!!
2022-07-05 15:03:00 【Java技术栈】
来源:cnblogs.com/yjmyzz/p/13562182.html
业务系统中,使用update语句更新数据是再正常不过的场景,我们也经常通过update更新的行数,来做一些业务判断,类似下面的伪代码:(mybatis + mysql 场景)
if (xxxMapper.updateByPrimaryKeySelective(entity)>0){ //更新成功,做其它业务处理}但是这里有一个坑,mysql中update影响行数>0是有条件的,假如有一张表:

里面只有一条记录(ID=1),我们用update更新一把ID=1的这条记录

这一行成功更新,影响行数为1,理所当然,然后把这条update语句再执行一次:

这时候,返回的影响行数为0,也就是说,当待更新的记录与原始记录旧值相同时,mysql其实并不会做任何更新。。
换言之,如果上游传过来的数据,与数据库本身的旧值相等,没有变化时,update语句影响行数为0。这与另一种场景:"更新一条并不存在的记录,影响行数返回0" 无法区分。

结论:不要使用update语句的影响行数做重要的业务判断!
近期热文推荐:
1.1,000+ 道 Java面试题及答案整理(2022最新版)
4.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
觉得不错,别忘了随手点赞+转发哦!
边栏推荐
- Interpretation of Apache linkage parameters in computing middleware
- I include of spring and Autumn
- 1330: [example 8.3] minimum steps
- Ionic Cordova project modification plug-in
- Behind the ultra clear image quality of NBA Live Broadcast: an in-depth interpretation of Alibaba cloud video cloud "narrowband HD 2.0" technology
- Calculate weight and comprehensive score by R entropy weight method
- Live broadcast preview | how to implement Devops with automatic tools (welfare at the end of the article)
- Number protection AXB function! (essence)
- ICML 2022 | 探索语言模型的最佳架构和训练方法
- I collect multiple Oracle tables at the same time. After collecting for a while, I will report that Oracle's OGA memory is exceeded. Have you encountered it?
猜你喜欢

Common PHP interview questions (1) (written PHP interview questions)

亿咖通科技通过ISO27001与ISO21434安全管理体系认证

NBA赛事直播超清画质背后:阿里云视频云「窄带高清2.0」技术深度解读

把 ”中台“ 的思想迁移到代码中去

Number protection AXB function! (essence)

Change multiple file names with one click

12 MySQL interview questions that you must chew through to enter Alibaba

Run faster with go: use golang to serve machine learning

MySQL----函数

Redis' transaction mechanism
随机推荐
Machine learning notes - gray wolf optimization
GPS original coordinates to Baidu map coordinates (pure C code)
你童年的快乐,都是被它承包了
The difference between SQL Server char nchar varchar and nvarchar
easyOCR 字符識別
[recruitment position] Software Engineer (full stack) - public safety direction
Number protection AXB function! (essence)
ICML 2022 | 探索语言模型的最佳架构和训练方法
社区团购撤城“后遗症”
MySQL之CRUD
Your childhood happiness was contracted by it
数据库学习——数据库安全性
"Sequelae" of the withdrawal of community group purchase from the city
把 ”中台“ 的思想迁移到代码中去
Calculate weight and comprehensive score by R entropy weight method
爱可可AI前沿推介(7.5)
超越PaLM!北大硕士提出DiVeRSe,全面刷新NLP推理排行榜
Redis' transaction mechanism
Array sorting num ranking merge in ascending order
Type declaration of all DOM elements in TS