问题描述:
我有三个表。表a,b,c,
每一个表都有两个字段,f1,f2;现在是我要用b表中的f1更新表a中的f1;
条件是记录在c表中:即只要b表的f2 = c表的f1 并且 a表的f2 = c表的f2我这样写为何不对:
update a set a.字段1 = b.字段1
from 表a a,表b b ,表c c
where a.字段2 = c.字段1 and b.字段2 = c.字段2

解决方案 »

  1.   

    具体需求就不看了。不过有个快捷的批量更新写法,而且支持多列更新。
    update (
    select yyy.a ya,zzz.b zb,yyy.b yb,zzz.b zb from yyy,zzz where yyy.c = zzz.c;
    ) set ya=za,yb=zb原理就是关联查询出来的结果作为虚拟表,然后批量update。
    局限性:用作关联的字段的值,一定都要是唯一特征的。比如这个字段是某个唯一编码表的外键。
      

  2.   

    Sql Server支持下面的这种写法
    update a set a.f2=b.f2 from a join b on a.f1=b.f1 
    where exists(select * from c where a.f1=c.f1)但是oracle不支持update 子句中再用from join好像,不知道还是用其他的方法
      

  3.   

    5楼的批量更新看不太懂,能否解释下?
    至于比较土的方法,可以用下面这个,oracle 9i通过update a set a.f2=(select f2 from b where a.f1=b.f1) 
    where exists(select * from c where a.f1=c.f1) and exists(select * from b where a.f1=b.f1)
      

  4.   


    update a 
     set a.字段1 = b.字段1 
    from 表a a,表b b ,表c c 
    where a.字段2 = c.字段2 and b.字段2 = c.字段1
      

  5.   

    update a a
       set a.f1= (select b.f1
                       from b b, c c
                      where b.f2 = c.f1 and a.f2=c.f2)
      

  6.   

    不过搂住的条件肯能出现满足条件的不只是1条的情况  可以再加个条件 and rownum <2
      

  7.   

    create table a(f1 int,f2 int);
    create table b(f1 int,f2 int);
    create table c(f1 int,f2 int);insert into a select 1,1 from dual;
    insert into a select 1,2 from dual;
    insert into b select 2,1 from dual;
    insert into b select 2,2 from dual;
    insert into c select 2,2 from dual;update a set f1=(select b.f1 from b,c where b.f2=c.f2 and a.f2=c.f1)
    where  exists(select 1 from b,c where b.f2=c.f2 and a.f2=c.f1);select * from a;drop table a;
    drop table b;
    drop table c;
    /*返回结果
    F1 F2
    1 1
    2 2
    */
      

  8.   

    update a set a.f2=(select f2 from b where a.f1=b.f1)
    where exists(select * from c where a.f1=c.f1) and exists(select * from b where a.f1=b.f1)
      

  9.   

    update a set f1=(select b.f1 from b,c where b.f2=c.f2 and a.f2=c.f1)
    where  exists(select 1 from b,c where b.f2=c.f2 and a.f2=c.f1);