Oracle to remove an association of several ways

Not much to say, we do the experiment right.

Create the following data table

select * from t1 ;

select * from t2;

Demand now: Referring to the table T2, T1 modify the table, modifying the contents of the condition to be consistent fname two columns of the table.

Mode 1, update

Common pitfalls:

UPDATE T1 
SET T1.FMONEY = (select T2.FMONEY from t2 where T2.FNAME = T1.FNAME)

After performing T1 results are as follows:

There is a line of the original value, is updated to a null value.

Correct wording:

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);

Embodiment 2: Update inline view

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

Mode 3: merge updates

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;

Guess you like

Origin www.cnblogs.com/duanjt/p/10945311.html