oracle sql更新字段为另一张表的对应字段

oracle sql更新字段为另一张表的对应字段
update table_1 t1 set t1.name=(select t2.name from table_2 where t1.id=t2.id and t1.idcard ='1111')
where exists (select 1 from table_2 where t1.id=t2.id and t1.idcard ='1111')
这是我写的sql 但是执行起来的执行次数是table_1的总条数 求高人解答 很急 现在加班中 求高人帮忙 几十万数据 要是这么执行执行次数就是天文数字了
首先你的语句有点问题,应该这样写:
update table_1 t1 set t1.name=(select t2.name from table_2 where t1.id=t2.id)
where exists (select 1 from table_2 where t1.id=t2.id) and t1.idcard ='1111';
之前子查询中的 t1.idcard ='1111' 是多余的。exists子句中的 t1.idcard ='1111'应该拿到外边来。

如果这样还是慢的话,那就是你的索引项建的有问题了,需要把两个表中的id作为索引,还有就是idcard也可以建索引。这样就好了。
我觉得应该使用merge比较好
merge into table_1 t1
using table_2 t2
on( t1.id=t2.id and t1.idcard ='1111')
when matched then
update name=t2.name;
自己手动敲得没有验证,如不懂用法的话可以百度merge用法看一下就会了
这很标准啊
update table_1 t1 set t1.name=(select t2.name from table_2 where t1.id=t2.id)不可以吗?
← 上一篇: 下一篇: →