第一種方法:
update tablea set column_name1=(select name2 from tableb where tableb.name3=tablea.name1)
只修改一個
update tablea set column_name1=(select name2 from tableb where tableb.name3='a') where tablea.name1='A'
第二種方法:
假設A表有字段ID和NameA,B表有字段ID和NameB,兩個表通過ID連接,把NameB更新到NameA,可以這麼寫:
merge into A
using(select NameB fromB) TMP
on (A.ID=TMP.ID)
when matched then
update set A.NameA=TMP.NameB
第三種方法:(建議使用這個)
- A表數據
- B表數據
- 現在要把B表 B_COSTS 的值update到A表 A_COSTS 字段
- SQL語法:
update a set (a.a_costs) = (select b.b_costs from b where a.id = b.id and a.a_id = b.b_id) where exists
(select 1 from b where a.id = b.id and a.a_id = b.b_id)
- 結果: