Merge into 详细介绍
2017-07-18 14:42
218 查看
Merge 语句是oracle9i新增的语法,用来合并update和insert语句
通过merge语句,根据一张表或子查询的连接条件对另外一张表进行查询,
连接条件匹配上的进行update操作,无法匹配的进行insert操作。
这个语法仅需要一次全表扫描就可以完成全部工作,执行效率高于insert。
语法:
MERGE INTO [your table-name] [rename your table here]
USING ( [write your query here] )[rename your query-sql and using just like a table]
ON ([conditional expression here] AND [...]...)
WHEN MATHED THEN [here you can execute some update sql or something else ]
WHEN NOT MATHED THEN [execute something else here ! ]
举例:
创建两个表 fzq1和fzq2
--全部男生记录
create table fzq1 as select * from fzq where sex='1';
--全部女生记录
create table fzq2 as select * from fzq where sex='0';
需求:
更新表fzq1 使得id相同的记录成绩字段+1,并且更新name 字段
如果id不同,则插入到表fzq1中
merg into fzq1 aa
useing fzq bb
on (aa.id=bb.id)
when matched then
update set
aa.chengji=bb.chengji+1,
aa.name=bb.name
when not matched then
insert value(bb.id,bb.name,bb.sex,bb.kecheng,bb.chengji);
需求2:将fzq1表中的女生记录的成绩+1
merge into fzq1 aa
using(select fzq.id,fzq.chengji from fzq join fzq2 on fzq.id=fzq2.id)bb
on (aa.id=bb.id)
when matched then
update set
aa.chengji=bb.chengji+1
不能做的事
merge into fzq1 aa
using fzq bb
on (aa.id=bb.id)
when matched then
update set
aa.id=bb.id+1
/*系统提示:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "AA"."ID"
我们不能更新on (aa.id=bb.id)关联条件中的字段*/
update fzq1
set id=(select id+1 from fzq where fzq.id=fzq1.id)
where id in
(select id from fzq)
--使用update就可以更新,如果有更好的方法,谢谢反馈!
通过merge语句,根据一张表或子查询的连接条件对另外一张表进行查询,
连接条件匹配上的进行update操作,无法匹配的进行insert操作。
这个语法仅需要一次全表扫描就可以完成全部工作,执行效率高于insert。
语法:
MERGE INTO [your table-name] [rename your table here]
USING ( [write your query here] )[rename your query-sql and using just like a table]
ON ([conditional expression here] AND [...]...)
WHEN MATHED THEN [here you can execute some update sql or something else ]
WHEN NOT MATHED THEN [execute something else here ! ]
举例:
创建两个表 fzq1和fzq2
--全部男生记录
create table fzq1 as select * from fzq where sex='1';
--全部女生记录
create table fzq2 as select * from fzq where sex='0';
需求:
更新表fzq1 使得id相同的记录成绩字段+1,并且更新name 字段
如果id不同,则插入到表fzq1中
merg into fzq1 aa
useing fzq bb
on (aa.id=bb.id)
when matched then
update set
aa.chengji=bb.chengji+1,
aa.name=bb.name
when not matched then
insert value(bb.id,bb.name,bb.sex,bb.kecheng,bb.chengji);
需求2:将fzq1表中的女生记录的成绩+1
merge into fzq1 aa
using(select fzq.id,fzq.chengji from fzq join fzq2 on fzq.id=fzq2.id)bb
on (aa.id=bb.id)
when matched then
update set
aa.chengji=bb.chengji+1
不能做的事
merge into fzq1 aa
using fzq bb
on (aa.id=bb.id)
when matched then
update set
aa.id=bb.id+1
/*系统提示:
ORA-38104: Columns referenced in the ON Clause cannot be updated: "AA"."ID"
我们不能更新on (aa.id=bb.id)关联条件中的字段*/
update fzq1
set id=(select id+1 from fzq where fzq.id=fzq1.id)
where id in
(select id from fzq)
--使用update就可以更新,如果有更好的方法,谢谢反馈!
相关文章推荐
- Oracle Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Merge into 详细介绍
- Oracle Merge into 详细介绍
- Oracle Merge into 详细介绍
- 引擎开发_ 碰撞检测_GJK 算法详细介绍
- js中获取时间new Date()详细介绍
- 图解CSS的padding,margin,border属性(详细介绍及举例说明)
- jQuery实用基础超详细介绍