根据另外两个表中的数据更新一个表

时间:2014-02-26 23:30:49

标签: sql oracle sql-update

我有3个表ADDRESS,PERS_INFO,ADDRESS_NEW以及以下虚拟数据

ADDRESS
-----------------------
ID | CITY | LINE1
21 |  abc |   def
22 |  fgh |   ijk


PERS_INFO
---------------------------------------
ID | FIRST_NAME | LAST_NAME | ADDR_ID
45 | Jennifer   | Higgins   | 21
46 | John       | Craig     | 22

ADDRESS_NEW
---------------------
ID | CITY | LINE1
1  | abc  | def
2  | fgh  | ijk

我想在Oracle中编写一个查询,以ADDR_ID替换PERS_INFO IDADDRESS_NEW个值的UPDATE PERS_INFO SET PERS_INFO.ADDR_ID = (SELECT ADDRESS_NEW.ID FROM ADDRESS_NEW, ADDRESS WHERE ADDRESS.ID = PERS_INFO.ADDR_ID); 值。 请记住,这只是虚拟数据,我想在UPDATE语句中编写没有任何数据的通用查询。请帮忙!!

这是我尝试过的代码。

{{1}}

1 个答案:

答案 0 :(得分:0)

尝试以下更新的查询:

update pers_info
   set pers_info.addr_id =
       (select y.id
          from (select address.*,
                       row_number() over(order by address.rowid) as link
                  from address
                  join pers_info
                    on address.id = pers_info.addr_id) x
          join (select address_new.*,
                      row_number() over(order by address.rowid) as link
                 from address_new
                 join pers_info
                   on address.id = pers_info.addr_id) y
            on x.link = y.link
         where x.id = pers_info.addr_id)