表格1:
id name desc ----------------------- 1 a abc 2 b def 3 c adf
表2:
id name desc ----------------------- 1 x 123 2 y 345
在oracle SQL中,如何运行 sql更新 查询,该查询可以使用表2name并desc使用表2来更新表1 id?所以我得到的最终结果是
name
desc
id
id name desc ----------------------- 1 x 123 2 y 345 3 c adf
这称为相关更新
UPDATE table1 t1 SET (name, desc) = (SELECT t2.name, t2.desc FROM table2 t2 WHERE t1.id = t2.id) WHERE EXISTS ( SELECT 1 FROM table2 t2 WHERE t1.id = t2.id )
假设联接导致键保留视图,您还可以
UPDATE (SELECT t1.id, t1.name name1, t1.desc desc1, t2.name name2, t2.desc desc2 FROM table1 t1, table2 t2 WHERE t1.id = t2.id) SET name1 = name2, desc1 = desc2