如何从Oracle的SQL中的其他表更新

时间:2019-08-03 13:14:09

标签: sql oracle sql-merge

我有两个表,我想从Oracle的sql中的表B更新表A。

table A
customer_id    geo_id     geo
1234567890       3521     texas
0987654321       3624     dallas
1597536842       3121     mexicocity
table B
geo_id        customer_id
8745          1234567890
2145          0987654321
3699          1597536842
update table A
set   geo_id   = (select geo_id from table B)
where tableA.customer_id = tableB.customer_id;

1 个答案:

答案 0 :(得分:4)

使用MERGE语句

MERGE INTO tablea a 
     using tableb b ON( a.customer_id = b.customer_id ) 
WHEN matched THEN 
  UPDATE SET a.geo_id = b.geo_id 

或相关更新

update tablea a set 
    a.geo_id = (select geo_id from 
                      tableb b 
                      where a.customer_id = b.customer_id)

DEMO