当前位置:网站首页>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边栏推荐
猜你喜欢

个人博客搭建与美化

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

Installing mongodb database in Windows Environment

机器人编程的培训学科类原理

数字IC设计流程总结

Construction and beautification of personal blog

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

解读创客教育所蕴含的科技素养

Training discipline principle of robot programming

QT5-布局在创作中的理解应用
随机推荐
Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images
Why build a personal blog
Principes de formation de la programmation robotique
基础知识之二——STA相关的基本定义
TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to
[learning notes] simple DP
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
自定义注解实现校验
Xjy-220/43ac220v static signal relay
【学习笔记】倍增 + 二分
【Qt5-基础篇】随机数显示屏展示
ESP8266 RC522
C# 自定义并动态切换光标
Why not two or four TCP handshakes
机器人编程的培训学科类原理
gin_gorm
About vctk datasets
Analyze the maker education path integrating the essence of discipline
DC學習筆記正式篇之零——綜述與基本流程介紹
Strictmode analysis activity leakage -strictmode principle (3)