表结构描述
待更新表 -表名 newTab
name | Value |
---|---|
电脑 | |
手机 | |
导管 |
数据来源表-表名 oldTab
goods | price |
---|---|
电脑 | $1600 |
手机 | $12 |
导管 | $1 |
需求描述
将oldTab表中的price值更新到newTab的value中去,关联关系是oldTab.goods=newTab.name
oracle sql写法
begin
for temp_tab in (select a.rowid,b.price
from newTab a,oldTab b
where a.name=b.goods
)
loop
update newTab set value = temp_tab .price where
rowid = temp_tab.rowid;
end loop;
end;
记录原因
用常规sql写法,嵌套查询,一个几万数据的表,草,更新花了半个多小时,还没执行完,而这个写法,妈的1秒不到,所以必须记一下。
常规sql类似如下:
update newTab a
set a.value=(select b.price from oldTab b where a.name=b.goods)
where exits (select 1 from oldTab c where a.name=c.goods );