当前位置:网站首页>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
边栏推荐
- Flutter nouvelle génération de rendu graphique Impeller
- Create a tabbar component under the components folder, which is public
- If I am PM's performance, movie VR ticket purchase display
- OSI层常用协议
- Some pits used by uni
- Golang通过exec模块实现Ping连通性检测案例
- shell指令学习1
- Traversal array, object parent-child communication props / $emit
- 3d slicer中拉直体的生成
- xxl-job采坑指南xxl-rpc remoting error(connect timed out)
猜你喜欢
Traversal array, object parent-child communication props / $emit
Flutter nouvelle génération de rendu graphique Impeller
Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios
Intel SGX preliminary learning and understanding notes (continuously updated)
Use of ES6 array
弘玑微课堂 | Cyclone RPA之“灵活的数字员工”执行器
Box collapse and margin collapse
On the use of constant pointer and pointer constant -- exercise (record)
Necessity of selenium preloading cookies
(11) Vscode code formatting configuration
随机推荐
Batch import of orange single micro service
Relative reference and absolute reference of Excel
Contract lock loophole
7-10 longest symmetric substring (25 points) (violence problem solution) C language
合约锁仓漏洞
Fast application fuzzy search
‘EddiesObservations‘ object has no attribute ‘filled‘
xxl-job采坑指南xxl-rpc remoting error(connect timed out)
Watch depth monitoring mode
uni使用的一些坑
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
mysql中duplicate key update
Solid contract DoS attack
Frequently asked interview questions - 2 (computer network)
弘玑|数字化时代下,HR如何进行自我变革和组织变革?
数据安全入门产品——数据库审计系统详解
转置卷积(Transposed Convolution)
C# ,类库
The title bar will be pushed to coincide with the status bar
refused connection