当前位置:网站首页>mysql中duplicate key update
mysql中duplicate key update
2022-04-23 05:35:00 【纸素画笺】
二、duplicate key update
duplicate key update (重复密钥更新)
插入字段时如何自我判断是更新还是插入
2.1基础用法
2.1.1 假设此时我们表中没有数据
执行语句
INSERT INTO test ( id, NAME )
VALUES
( '1', '晓明' )
ON DUPLICATE KEY UPDATE
id='123',
name='xiaoming'
结果
2.1.2 有数据后再次执行
执行语句
同上
sql结果
这里就是关键:我们可以看到变动为2,此时就是说明原数据进行了更新,更新内容为下面设置的字段值
结果
Q:我们再次执行一次这个语句,结果是什么样子呢?
A:自然是判断出该表中无此数据,新增一条额外的新数据
sql结果
结果
小结
到这里大家肯定明白了,其实就是会自动检测是否存在,如果存在values后面的值就会自动更改,不存在则插入
2.3 拓展用法,批量插入
执行语句
INSERT INTO test(`id`,`name`,`address`)
VALUES('4','修改10','北京'),
( '1', '晓明' ,1)
ON DUPLICATE KEY UPDATE
name=VALUES(name),address=VALUES(address);
结果
批量自动判断是否存在,存在则自动修改
2.4 Mybatis中的写法
单独插入
<insert id="insertUser" parameterType="com.test.User">
INSERT INTO user(
id,
name,
gender,
birthday,
address)
VALUES
(#{id},
#{name},
#{gender},
#{birthday},
#{address})
ON DUPLICATE KEY UPDATE
id = VALUES(id),
name = VALUES(name),
gender = VALUES(gender)
birthday = VALUES(birthday),
address = VALUES(address)
</insert>
批量插入
<insert id="insertUser" parameterType="java.util.List">
INSERT INTO user(
id,
name,
gender,
birthday,
address)
VALUES
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id},
#{item.name},
#{item.gender},
#{item.birthday},
#{item.address})
</foreach>
ON DUPLICATE KEY UPDATE
id = VALUES(id),
name = VALUES(name),
gender = VALUES(gender)
birthday = VALUES(birthday),
address = VALUES(address)
</insert>
版权声明
本文为[纸素画笺]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42331108/article/details/124341379
边栏推荐
- Frequently asked interview questions - 1 (non technical)
- Pavlov and hobbies
- Camera imaging + homography transformation + camera calibration + stereo correction
- catkin_package到底干了什么
- Tslint annotations ignore errors and restful understanding
- Common interview questions - 4 (MySQL)
- C语言——恶搞关机小程序
- Hongji | how does HR carry out self change and organizational change in the digital era?
- 世界与个人发展
- 可執行程序執行流程
猜你喜欢
Hongji | how does HR carry out self change and organizational change in the digital era?
Frequently asked interview questions - 2 (computer network)
what is wifi6?
Data mining -- understanding data
es6数组的使用
After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner
How to set the initial value of El input number to null
selenium預先加載cookie的必要性
selenium预先加载cookie的必要性
Excel sets row and column colors according to cell contents
随机推荐
QT displays the specified position and size of the picture
Similarities and differences between vector and array (notes)
Pytorch deep learning practice_ 11 convolutional neural network
Camera imaging + homography transformation + camera calibration + stereo correction
Double click The jar package cannot run the solution
Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator
Fast application fuzzy search
IPI interrupt
Knowledge of egg testing -- mock, Supertest, coffee
uni使用的一些坑
Arithmetic and logical operations
Multi process model in egg -- egg document Porter
Edit, cancel, pull up menu
合约锁仓漏洞
How to set the initial value of El input number to null
The QT debug version runs normally and the release version runs crash
Phlli in a VM node
shell指令学习1
Intel SGX preliminary learning and understanding notes (continuously updated)
SQL语句简单优化