当前位置:网站首页>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边栏推荐
- The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?
- 软件开发完整流程
- Basic knowledge II - Basic definitions related to sta
- 【Qt5-基础篇】随机数显示屏展示
- Impact relay zc-23/dc220v
- Poor students can also play raspberry pie
- QT5-布局在创作中的理解应用
- 用Steam教育启发学生多元化思维
- 小程序自定义宫格
- Unknown database连接数据库错误
猜你喜欢

Basic knowledge II - Basic definitions related to sta

探索互联网时代STEAM教育创新之路

Analyze the maker education path integrating the essence of discipline

Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.

Exploring the road of steam education innovation in the Internet Era

K210 site helmet

QT5-布局在创作中的理解应用
![奇偶链表[链表操作的两种大方向]](/img/4e/ce860bc172bb75f456427ba26a7842.png)
奇偶链表[链表操作的两种大方向]

Orb-slam2 source code learning (II) map initialization

微研所,微生物检验中常用的生化反应
随机推荐
Open3d point cloud color rendering
Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
技术人进阶画业务大图,手把手教学来了
pull_ to_ refresh
Why not two or four TCP handshakes
友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
数字IC设计流程总结
Interpreting the scientific and technological literacy contained in maker Education
Pytorch programming knowledge (2)
做生意更加务实
Analyzing the wisdom principle in maker education practice
DC学习笔记正式篇之零——综述与基本流程介绍
dc_ Study and summary of labs--lab1
System.CommandLine版CSRebot
JS方法大全的一个小文档
Two position relay st2-2l/ac220v
Orb-slam2 source code learning (II) map initialization
45岁程序员告诉你:程序员为什么要跳槽,太真实...
Use of typora
TypeError: Argument ‘angle‘ can not be treated as a double