网站首页 > 教程文章 正文
由于工作中使用,研究了merge into 语句是insert 与update语句的结合,可以同时实现update和insert的功能。
一、merge into语句的语法。
MERGE INTO schema. table alias
USING { schema. table | views | query} alias
ON {(condition) }
WHEN MATCHED THEN
UPDATE SET {clause}
WHEN NOT MATCHED THEN
INSERT VALUES {clause};
--解析
INTO 子句
用于指定你所update或者Insert目的表。
USING 子句
用于指定你要update或者Insert的记录的来源,它可能是一个表,视图,子查询。
ON Clause
用于目的表和源表(视图,子查询)的关联,如果匹配(或存在),则更新,否则插入。
merge_update_clause
用于写update语句
merge_insert_clause
用于写insert语句
二、merge 语句的各种用法练习
创建表及插入记录
create table t_B_info_aa
( id varchar2(20),
name varchar2(50),
type varchar2(30),
price number
);
insert into t_B_info_aa values('01','冰箱','家电',2000);
insert into t_B_info_aa values('02','洗衣机','家电',1500);
insert into t_B_info_aa values('03','热水器','家电',1000);
insert into t_B_info_aa values('04','净水机','家电',1450);
insert into t_B_info_aa values('05','燃气灶','家电',800);
insert into t_B_info_aa values('06','太阳能','家电',1200);
insert into t_B_info_aa values('07','西红柿','食品',1.5);
insert into t_B_info_aa values('08','黄瓜','食品',3);
insert into t_B_info_aa values('09','菠菜','食品',4);
insert into t_B_info_aa values('10','香菇','食品',9);
insert into t_B_info_aa values('11','韭菜','食品',2);
insert into t_B_info_aa values('12','白菜','食品',1.2);
insert into t_B_info_aa values('13','芹菜','食品',2.1);
create table t_B_info_bb
( id varchar2(20),
type varchar2(50),
price number
);
insert into t_B_info_bb values('01','家电',2000);
insert into t_B_info_bb values('02','家电',1000);
1) update和insert同时使用
merge into t_B_info_bb b
using t_B_info_aa a --如果是子查询要用括号括起来
on (a.id = b.id and a.type = b.type) --关联条件要用括号括起来
when matched then
update set b.price = a.price --update 后面直接跟set语句
when not matched then
insert (id, type, price) values (a.id, a.type, a.price) --insert 后面不加into
---这条语句根据t_B_info_aa 更新了t_B_info_bb中的一条记录,插入了11条记录
2)只插入不更新
--处理表中数据,使表中的一条数据发生变化,删掉一部分数据。用来验证只插入不更新的功能
update t_B_info_bb b set b.price=1000 where b.id='02';
delete from t_B_info_bb b where b.type='食品';
--只是去掉了when matched then update语句
merge into t_B_info_bb b
using t_B_info_aa a
on (a.id = b.id and a.type = b.type)
when not matched then
insert (id, type, price) values (a.id, a.type, a.price)
3)只更新不插入
--处理表中数据,删掉一部分数据,用来验证只更新不插入
delete from t_B_info_bb b where b.type='食品';
--只更新不插入,只是去掉了when not matched then insert语句
merge into t_B_info_bb b
using t_B_info_aa a
on (a.id = b.id and a.type = b.type)
when matched then
update set b.price = a.price
三、加入限制条件的操作
变更表中数据,便于练习
update t_B_info_bb b
set b.price = 1000
where b.id='02'
delete from t_b_info_bb b where b.id not in ('01','02') and b.type='家电'
update t_B_info_bb b
set b.price = 8
where b.id='10'
delete from t_b_info_bb b where b.id in ('11','12','13') and b.type='食品'
表中数据
执行merge语句:脚本一和脚本二执行结果相同
脚本一: merge into t_B_info_bb b
using t_B_info_aa a
on (a.id = b.id and a.type = b.type)
when matched then
update set b.price = a.price where a.type = '家电'
when not matched then
insert
(id, type, price)
values
(a.id, a.type, a.price) where a.type = '家电';
----------------------
脚本二: merge into t_B_info_bb b
using (select a.id, a.type, a.price
from t_B_info_aa a
where a.type = '家电') a
on (a.id = b.id and a.type = b.type)
when matched then
update set b.price = a.price
when not matched then
insert (id, type, price) values (a.id, a.type, a.price);
结果:
上面两个语句是只对类型是家电的语句进行插入和更新
而脚本三是只对家电进行更新,其余的全部进行插入
merge into t_B_info_bb b
using t_B_info_aa a
on (a.id = b.id and a.type = b.type)
when matched then
update set b.price = a.price
where a.type = '家电'
when not matched then
insert
(id, type, price)
values
(a.id, a.type, a.price);
四、加删除操作
update子句后面可以跟delete子句来去掉一些不需要的行
delete只能和update配合,从而达到删除满足where条件的子句的记录
例句:
merge into t_B_info_bb b
using t_B_info_aa a
on (a.id=b.id and a.type = b.type)
when matched then
update set b.price=a.price
delete where (a.type='食品')
when not matched then
insert values (a.id, a.type, a.price) where a.id = '10'
————————————————
猜你喜欢
- 2025-04-06 超级详细的zabbix环境搭建和测试(看我的教程,大神你也可以)
- 2025-04-06 SQL——UPDATE:更新数据(更新sql语句)
- 2025-04-06 什么是 SQL 事务,如何创建 SQL 事务
- 2025-04-06 Win11学院:如何修复Windows Update服务丢失问题
- 2025-04-06 TiDB 专用语法,优化器注释和表属性
- 2025-04-06 20000 字干货笔记,一天搞定 MySQL
- 2025-04-06 数据库:Mysql中“select ... for update”排他锁分析
- 2025-04-06 【Mybatis实战第4天】三步搞定 Mybatis更新(update)操作
- 2025-04-06 select...for update到底是加了行锁,还是表锁?
- 2025-04-06 超详细的Oracle19c修改数据库用户名教程
- 最近发表
-
- Nacos最全详解(8大架构原理图解)(nacos原理讲解)
- 干货!微服务全量升级JDK17,踩坑笔记整理
- springboot集成nacos配置中心(springcloud配置nacos集群)
- Nacos快速入门指南(nacos 使用)
- Nacos读取配置文件的顺序(nacos 配置文件)
- 本地启动若依项目nacos报错:Server check fail, please check server
- 阿里Nacos惊爆安全漏洞,火速升级!(附修复建议)
- Nacos简介—1.Nacos使用简介(nacos是干啥的)
- Nacos简介—4.Nacos架构和原理一(nacos原理讲解)
- Nacos架构最全详解(图文全面总结)
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)