码农的世界Java

批量更新语句

2019-09-25  本文已影响0人  一觉睡到丶小时候
update eawbpre e
   set e.eawb_handletime =
       (select ep.eawb_handletime
          from temp_eawbpre_test ep
         where e.eawb_reference1 = ep.eawb_reference1)
 where exists (select 1
          from temp_eawbpre_test ep
         where e.eawb_reference1 = ep.eawb_reference1);

错误的写法

update table_name t1 set (a,b,c)=( select a,b,c from table_name_2 t2 where t1.a=t2.a);
这种写法,会更新t1表中的所有行:如果t1.a=t2.a的,就更新t2中查出的记录进t1;如果t1.a<>t2.a的,t1中的记录会被更新成空(null)

正确的写法

update table_name t1 
set (a,b,c)=( select a,b,c from table_name_2 t2 where t1.a=t2.a)
where exists(
      select 1 from table_name_2 t2 where t1.a=t2.a
);  

解析
正确的写法,就是在后面加了一句 where exists(select 1 from table_name_2 t2 where t1.a=t2.a);
这句话的意思是:如果存在t1.a=t2.a,就更新,否则,不更新,所以不会导致t1表中所有的记录都被更新。

上一篇 下一篇

猜你喜欢

热点阅读