当前位置:网站首页>mysql使用on duplicate key update批量更新数据
mysql使用on duplicate key update批量更新数据
2022-07-31 04:47:00 【jiey0407】
创建测试—book科目表
CREATE TABLE `book` (
`id` int NOT NULL AUTO_INCREMENT,
`unique_code` varchar(30) NOT NULL,
`book_name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_code` (`unique_code`) USING BTREE COMMENT 'book unique_code'
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
前提需要表中存在索引:如下 unique_code 即为索引

需求:需要导入数据到book表。 当book表中存在科目的数据时,update数据值,否则insert插入一条新记录。
以往做法:循环select表中的booke记录是否存在,存在则使用update;不存在则使用insert。
做法弊端:每处理一条记录需要操作两次数据库(select、update/insert)
优化做法:使用insert语句搭配 on duplicate key update使用。
做法注意:比如上面的需求,需要用到 book表****唯一unique_code索引
一、执行如下命令
insert into book(
unique_code,
book_name
) values (
'zs-001',
'中文'
) on duplicate key update book_name='数学';

二、再次执行步骤一命令你会发现book_name值改变

简单明白,通俗易懂~~
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- C language confession code?
- qlib自动化quant
- Reinforcement learning: from entry to pit to shit
- Solved (the latest version of selenium framework element positioning error) NameError: name 'By' is not defined
- qlib架构
- Create componentized development based on ILRuntime hot update
- MySQL忘记密码怎么办
- 微软 AI 量化投资平台 Qlib 体验
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- MATLAB/Simulink & & STM32CubeMX tool chain completes model-based design development (MBD) (three)
猜你喜欢

城市内涝及桥洞隧道积水在线监测系统

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法

Industry landing presents new progress | 2022 OpenAtom Global Open Source Summit OpenAtom OpenHarmony sub-forum was successfully held

ENSP, VLAN division, static routing, comprehensive configuration of Layer 3 switches

MySQL database must add, delete, search and modify operations (CRUD)

【wpf】wpf中的那些模板之深度解析

The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands

PWN ROP

开源汇智创未来 | 2022开放原子全球开源峰会OpenAtom openEuler分论坛圆满召开

pom文件成橘红色未加载的解决方案
随机推荐
Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held
【R语言】【3】apply,tapply,lapply,sapply,mapply与par函数相关参数
open failed: EACCES (Permission denied)
PCL calculates the point cloud coordinate maximum and its index
C Implementation of Simple Network File Copy
HCIP第十天_BGP路由汇总实验
Unity框架设计系列:Unity 如何设计网络框架
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
Lua,ILRuntime, HybridCLR(wolong)/huatuo热更新对比分析
Explanation of
[py script] batch binarization processing images
论治理与创新 | 2022开放原子全球开源峰会OpenAnolis分论坛圆满召开
高等数学---第九章二重积分
.NET-6.WinForm2.NanUI学习和总结
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
The 15th day of the special assault version of the sword offer
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)
Two address pools r2 are responsible for managing the address pool r1 is responsible for managing dhcp relays