我有table1
和table2
。它们具有相同的列,列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
);
答案 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