很少說了,咱們來作實驗吧。code
建立以下表數據blog
select * from t1 ;
select * from t2;
現需求:參照T2表,修改T1表,修改條件爲兩表的fname列內容一致。class
常見陷阱:date
UPDATE T1 SET T1.FMONEY = (select T2.FMONEY from t2 where T2.FNAME = T1.FNAME)
執行後T1結果以下:select
有一行原有值,被更新成空值了。im
正確寫法:d3
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);
UPDATE ( select t1.fmoney fmoney1,t2.fmoney fmoney2 from t1,t2 where t1.fname = t2.fname )t set fmoney1 =fmoney2;
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;