使用另一个表中的值更新一个表中的值

时间:2018-02-07 21:59:51

标签: sql oracle join sql-update

我有table1table2。它们具有相同的列,列ID是我可用于连接表的列。

如何使用Name中的table1列的值更新Name中的行table2的foreach语句? 我需要这个,所以我可以修复Name中的Table1列,因为它是incorect,它的好值是table2

我尝试使用单个更新语句,但执行需要永远,因为两个表都有超过600 000行

 update 
  table1 t1
set
  (
    t1.name
      ) = (
    select
      t2.name
    from
      table2  t2
    where
      t2.id = t1.id
    and
      rownum = 1    
     )
    where exists (
      select 
        null
      from 
        table2 t2
      where 
        t2.id = t1.id
      ); 

2 个答案:

答案 0 :(得分:1)

对于此查询:

update table1 t1
    set t1.name = (select t2.name from table2  t2 where t2.id = t1.id and  rownum = 1)
    where exists (select 1
                  from table2 t2
                  where t2.id = t1.id
                 ); 

您需要table2(id, name)上的索引。

答案 1 :(得分:-1)

一个简单的内连接应该可以解决这个问题。

UPDATE T1
SET T1.NAME = T2.NAME 
FROM MyTable T1
INNER JOIN MyOtherTable T2 
ON T1.ID = T2.ID