当前位置:网站首页>Duplicate key update in MySQL
Duplicate key update in MySQL
2022-04-23 05:39:00 【Plain paper】
Two 、duplicate key update
duplicate key update ( Duplicate key update )
How to judge whether to update or insert when inserting fields
2.1 Basic usage
2.1.1 Suppose there is no data in our table at this time
Execute statement
INSERT INTO test ( id, NAME )
VALUES
( '1', ' Xiaoming ' )
ON DUPLICATE KEY UPDATE
id='123',
name='xiaoming'
result

2.1.2 Execute again when there is data
Execute statement
ditto
sql result

Here's the key : We can see the change as 2, This means that the original data has been updated , The update content is the field value set below
result

Q: Let's execute this statement again , What is the result ?
A: Naturally, it is judged that there is no such data in the table , Add an additional new data
sql result

result

Summary
You must understand by now , In fact, it will automatically detect whether it exists , If there is values The following values will change automatically , Insert if it doesn't exist
2.3 Expand usage , Batch insert
Execute statement
INSERT INTO test(`id`,`name`,`address`)
VALUES('4',' modify 10',' Beijing '),
( '1', ' Xiaoming ' ,1)
ON DUPLICATE KEY UPDATE
name=VALUES(name),address=VALUES(address);
result

Batch automatically judge whether there is , If it exists, it will be modified automatically
2.4 Mybatis Writing in Chinese
Insert separately
<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>
Batch 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>
版权声明
本文为[Plain paper]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230534486650.html
边栏推荐
- QSS, qdateedit, qcalendarwidget custom settings
- Golang通过exec模块实现Ping连通性检测案例
- uni使用的一些坑
- TypeScript interface & type 粗略理解
- Strategies to improve Facebook's touch rate and interaction rate | intelligent customer service helps you grasp users' hearts
- Common interview questions - 4 (MySQL)
- If I am PM's performance, movie VR ticket purchase display
- On the use of constant pointer and pointer constant -- exercise (record)
- Sword finger offer II 022 The entry node of the link in the linked list
- Several examples of pointer transfer, parameter transfer, value transfer, etc
猜你喜欢

Common protocols of OSI layer

Some pits used by uni

Double click The jar package cannot run the solution

(11) Vscode code formatting configuration

Pol / select / EPO

弘玑|数字化时代下,HR如何进行自我变革和组织变革?

Establish excel bookkeeping book through setting context menu

Create cells through JS (while loop)

Excel sets row and column colors according to cell contents

OSI层常用协议
随机推荐
Contract lock loophole
C language - Spoof shutdown applet
Various situations of data / component binding
Hongji | how does HR carry out self change and organizational change in the digital era?
Rog attack
Formal parameters, local variables and local static variables
QSS, qdateedit, qcalendarwidget custom settings
Camera imaging + homography transformation + camera calibration + stereo correction
Redis经典面试题总结2022
Issue 36 summary of atcoder beginer contest 248
Differences between auto and decltype inference methods (learning notes)
Intel SGX preliminary learning and understanding notes (continuously updated)
五一劳动节期间什么理财产品会有收益?
refused connection
Linear sieve method (prime sieve)
Usage and difference of shellexecute, shellexecuteex and winexec in QT
Necessity of selenium preloading cookies
Excel sets row and column colors according to cell contents
Breadth first search topics (BFS)
STD:: String implements split