Oracle两表关联更新三种方式

创建如下表数据

select * from t1 ;

select * from t2;

现需求:参照T2表,修改T1表,修改条件为两表的fname列内容一致。

方式1. update

UPDATE T1 
SET T1.FMONEY = (select T2.FMONEY from t2 where T2.FNAME = T1.FNAME)
WHERE EXISTS(SELECT 1 FROM T2 WHERE T2.FNAME = T1.FNAME);

方式2. 内联视图更新

UPDATE (
select t1.fmoney  fmoney1,t2.fmoney  fmoney2 from t1,t2 where t1.fname = t2.fname
)t
set fmoney1 =fmoney2;

方式3. merge更新

merge into t1
using (select t2.fname,t2.fmoney from t2) t
on (t.fname = t1.fname)
when matched then 
update  set t1.fmoney = t.fmoney;

未经允许不得转载:王超博客 » Oracle两表关联更新三种方式

赞 (0)

相关推荐

    暂无内容!