现有 表 A 字段为 a1 ,a2,a3,a4 主键为 a1
表B 字段跟A一样,但是表B 只有表A的部分数据(表B中的a1全部来自A表),
现在表B的a4 字段有做修改,
如何用B表中的a4来修改A中的a4字段
现在用的语句如下:
update A t set t.a4 =(select b.a4 from B where A.a1=B.a4)
where A.a1 in (select b.a1 from B where A.a1=B.a4) ;
总觉得这条语句有点怪,但是 又不能删除where 条件 ,否则 如果A表中a1不在B表中的数据的所有a4将变为空

解决方案 »

  1.   

    update A t set t.a4 = (select nvl(b.a4,t.a3)  from B b where t.a1 = b.a1)这种语句行不通,为什么
      

  2.   

    按照你的需求,你的语句中 A.a1=B.a4 应该是笔误吧, A.a1=B.a1?
    你看这个行不行
    update A
    set A.a4 =
          (select B.a4
           from B
           where B.a1 = A.a1)
    where exists
            (select '1'
             from B
             where A.a1 = B.a1);
      

  3.   

    update a set a.a4 = b.a4
     from a
     join b on a.a1 = b.a1 and a.a4 <> b.a4
      

  4.   

    update A t set t.a4 = (select nvl(b.a4,t.a3) from B b where t.a1 = b.a1)
    你到B表中去查询t(A)的字段t.a3,能够行吗???????
    select nvl(b.a4,t.a3) from B b where t.a1 = b.a1
      

  5.   

    update A t set t.a4 = nvl((select b.a4 from B b where t.a1 = b.a1), t.a3);
      

  6.   

      1 update A set A.name=(select B.name from B where A.id=B.id) where
     exists (select 1 from B where A.id=B.id);

      2 [color=#808000]update A set A.name = nvl((select B.name from B where A.id=B.id),A.name);[/color]   这两种方式都能实现,但是发现更新的行数,还是有所不同的,第一种是只更新查到的不同的数据
    ;第二种更新了全部,就是说即使数据B中没有A的数据,A中还会在进行一次自我更新.
    所以效率第二种高
      

  7.   

    [Quote=引用 12 楼 zhaolinzzu 的回复:]
    1 update A set A.name=(select B.name from B where A.id=B.id) where
    exists (select 1 from B where A.id=B.id);
      2 update A set A.name = nvl((select B.name from B where A.id=B.id),A.nam……
    所以效率第一种高