Oracle将一个表的字段更新到另外的一个表的字段

在开发的过程中,我们经常会碰到这样的需求,将一个表的某个字段数据更新到另外的表字段(两个表有关联)

table_1

t_id real_name
1 张三
2 李四
3 王五

table_2

id t_id name
88 1 赵六
96 2
54 3

把 table_1 的 real_name 更新到 table_2 的name字段中

语句(table_2 表数据比 table_1小):

UPDATE table_2 t2
   SET (t2.name) = (SELECT t1.real_name
                            
                              FROM table_1 t1
                            
                             where t1.t_id= t2.t_id)

 WHERE t2.t_id IN (SELECT t1.t_id FROM table_1 t1)
 --后续条件

或者(table_2 表数据比 table_1大)

UPDATE table_2 t2
   SET (t2.name) = (SELECT t1.real_name
                            
                              FROM table_1 t1
                            
                             where t1.t_id= t2.t_id)

 WHERE t2.t_id exists (SELECT t1.t_id FROM table_1 t1 where t1.t_id = t2.t_id)
 --后续条件


整理来源:https://blog.csdn.net/coy52296/article/details/100234332

猜你喜欢

转载自blog.csdn.net/qq_46051303/article/details/128287613