有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value换为A中对应的value网上大多数参考答案如下:
update   b   set   b.value=(select   a.value   from   a   where   a.key=b.key)   where   b.id   in(select   b.id   from   b,a   where   b.key=a.key);
但本人模拟试验后发现结果是‘悲哀’的 :提示“单行子查询返回多个行 ”的错误  期待高人指点  给予真确解答

解决方案 »

  1.   

    update b set b.value=(select a.value from a where a.key=b.key) where exists (select 1 from b,a where b.key=a.key)
      

  2.   

    update b set b.value=(select a.value from a where a.key=b.key and rownum<2) where exists(select 1 from a where b.key=a.key);出现这个错误的原因是a表中key字段值不唯一
      

  3.   

    update a set value=(select value from  b where a.key=b.key) where exists(select 1 from b where a.key=b.key);如果,还报那个错误的话,说明你的a中每一个key 对应的value 值不止一个
      

  4.   

    多谢各位!
    update b set b.value=(select a.value from a where a.key=b.key) where b.value in(select b.value from b,a where b.key=a.key);    貌似这个才是正确的
      

  5.   

    最好用exists,in的效率比较低,尽量少使用
      

  6.   

    ]update tbb b set b.value=(select a.value from tba a where a.key=b.key and rownum=1) 
    where exists(select 1 from tba c where c.key=b.key)
    --a表中的数据可能是一个key有几个valueupdate tbb b set b.value=(select max(a.value) from tba a where a.key=b.key group by a.key) 
    where exists(select 1 from tba c where c.key=b.key)
      

  7.   

    有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value换为A中对应的value
    create table a(key int,value int)
    create table b(key int,value int)insert into a values(1,1)
    insert into a values(2,1)
    insert into a values(3,1)
    insert into a values(4,1)
    insert into a values(5,1)
    insert into a values(9,1)insert into b values(3,3);
    insert into b values(4,4);
    insert into b values(5,5);
    insert into b values(6,6);
    insert into b values(7,7);
    insert into b values(8,8);答案  update   b   set   b.value=(select   a.value   from   a  where   a.key=b.key)   where   b.key   in(select   b.key   from   b,a   where   b.key=a.key);  为什么子查询可以从一个表去差
      

  8.   

    若A,B中的KEY 都是唯一的话,则以上的代码是可以执行的。key唯一的话,语句可以 这样 写update b set value=a.value from a where a.key=b.key
    若KEY有一个以上的话,则就会出错。。update b set value=(select value from a where a.key=b.key and rownum=1)
               where b.key in(select key from a);
    以上的代码有一个不好的地方就是从a表中返回的 value值不唯一,只是返回其中的每一条.
    将你的代码的后半部分进行了优化(where b.key in(select key from a);就可以 了,不需要再进行联接了
      

  9.   


    如果可以确定key值的唯一性可以跳过oracle唯一约束检查。
    update
    (
    select/*+bypass_ujvc*/ a.value as val_a,b.value as val_b
    from a,b
    where a.key = b.key
    )tmp
    set tmp.val_b = tmp.val_a;
      

  10.   

    楼上说了应该使用 
    update taba a set col = (select col from tabb b where a.key=b.key )