当前位置:网站首页>mysql插入\更新前+判断条件
mysql插入\更新前+判断条件
2022-07-01 00:42:00 【长春小霸王】
1、mysql插入前判断数据是否存在的操作
INSERT INTO table(field1, field2, fieldn) SELECT 'field1',
'field2', 'fieldn' FROM DUAL WHERE NOT EXISTS(SELECT field FROM
table WHERE field = ?)
2、不存在则插入,存在则更新
如果插入的数据会导致UNIQUE 索引或PRIMARY KEY发生冲突/重复,则执行UPDATE语句
INSERT INTO `student`(`name`, `age`) VALUES('Jack', 19)
ON DUPLICATE KEY
UPDATE `age`=19;
-- If will happen conflict, the update statement is executed 2 row(s) affected
3、如果插入的数据会导致UNIQUE 索引或PRIMARY KEY发生冲突/重复,则先删除旧数据再插入最新的数据
REPLACE INTO `student`(`name`, `age`) VALUES('Jack', 18);4、避免重复插入
关键字/句:insert ignore into,如果插入的数据会导致UNIQUE索引或PRIMARY KEY发生冲突/重复,则忽略此次操作/不插入数据
INSERT IGNORE INTO `student`(`name`, `age`) VALUES('Jack', 18);
-- 0 row(s) affected边栏推荐
- 【qt5-tab标签精讲】Tab标签及内容分层解析
- WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
- ASCII、Unicode、GBK、UTF-8之间的关系
- Use of typora
- TypeError: Argument ‘angle‘ can not be treated as a double
- Dls-42/6-4 dc110v double position relay
- StrictMode卡顿与泄漏检测-StrictMode原理(2)
- None of the following candidates is applicable because of a receiver type mismatch
- About the general input operation mode of unity
- StrictMode带来的思考-StrictMode原理(5)
猜你喜欢

Zero of DC learning notes -- overview and basic process introduction

Windows环境下安装MongoDB数据库

Unknown database连接数据库错误

Koa koa combine routes sub route management

Interpreting the scientific and technological literacy contained in maker Education

DC學習筆記正式篇之零——綜述與基本流程介紹

Two position relay st2-2l/ac220v

ESP8266 RC522

neo4j安装、运行以及项目的构建和功能实现

Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
随机推荐
K210 site helmet
機器人編程的培訓學科類原理
Training discipline principle of robot programming
二季度最后一天
None of the following candidates is applicable because of a receiver type mismatch
pull_ to_ refresh
js中把数字转换成汉字输出
For the first time in more than 20 years! CVPR best student thesis awarded to Chinese college students!
QT5-布局在创作中的理解应用
Construction and beautification of personal blog
Kongyiji's first question: how much do you know about service communication?
[learning notes] simple DP
qt5-MVC:数据可视化的层次揭秘
sort自定义函数
TypeError: Argument ‘angle‘ can not be treated as a double
StrictMode分析Registion-StrictMode原理(4)
Exploration and practice of "flow batch integration" in JD
Looksrare team's "cash out" caused disturbance
neo4j安装、运行以及项目的构建和功能实现
One of the basics - overview of sta Basics