当前位置:网站首页>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# 实现PLC的定时器
- 【C语言进阶】文件操作(一)
- The MySQL database installed configuration nanny level tutorial for 8.0.29 (for example) have hands
- MySQL数据库增删改查(基础操作命令详解)
- XSS shooting range (3) prompt to win
- 开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
- Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
- 微软 AI 量化投资平台 Qlib 体验
- ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
- ES 源码 API调用链路源码分析
猜你喜欢
【云原生】DevOps(五):集成Harbor
从零开始,一镜到底,纯净系统搭建除草机(Grasscutter)
MySQL database backup
Unity框架设计系列:Unity 如何设计网络框架
Open Source Database Innovation in the Digital Economy Era | 2022 Open Atom Global Open Source Summit Database Sub-Forum Successfully Held
input输入框展示两位小数之precision
Win10 CUDA CUDNN 安装配置(torch paddlepaddle)
[debug highlights] Expected input batch_size (1) to match target batch_size (0)
Vue项目通过node连接MySQL数据库并实现增删改查操作
三道leetcode上的oj题
随机推荐
Error EPERM operation not permitted, mkdir ‘Dsoftwarenodejsnode_cache_cacach两种解决办法
mysql数据库安装(详细)
Unity URP渲染管线摄像机核心机制剖析
已解决:不小心卸载pip后(手动安装pip的两种方式)
PCL calculates the point cloud coordinate maximum and its index
【小土堆补充】Pytorch学习笔记_Anaconda虚拟环境使用
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
问题7:列表的拼接
open failed: EACCES (Permission denied)
Blockbuster | foundation for platinum, gold, silver gave nameboards donors
问题1:给你1-10的列表,实现列表输出,单数在左边,双数在右边。
专访 | 阿里巴巴首席技术官程立:云+开源共同形成数字世界的可信基础
高斯分布及其极大似然估计
unity2d game
Unity资源管理系列:Unity 框架如何做好资源管理
ERROR 1819 (HY000) Your password does not satisfy the current policy requirements
Sun Wenlong, Secretary General of the Open Atom Open Source Foundation |
SOLVED: After accidentally uninstalling pip (two ways to manually install pip)
Interview | Cheng Li, CTO of Alibaba: Cloud + open source together form a credible foundation for the digital world
Open Source Smart Future | 2022 OpenAtom Global Open Source Summit OpenAtom openEuler sub-forum was successfully held