表1
字段:id    a1     a2    a3
      1     ll     ll    ll
      2     hh     hh    hh表2
字段:id     f1    f2
      1      j     d
      1      k    dd
      1      l     fd
      2      j     fn  
      2      k     fe表1与表2通过id相联,表1主键id,表2主键为f2
把a1的内容更新到f1
结果
字段:id     f1    f2
      1      ll     d
      1      ll    dd
      1      ll     f
      2      hh   f  
      2      hh   f
要求只写sql,这个该怎么写
哎,小弟我才注册没分了,恳请帮助啊

解决方案 »

  1.   

    update table2 a set a.f1 = (select b.a1 from table1 b where b.id=a.id)
    where exists (select 1 from table1 b where b.id=a.id);
      

  2.   

    exists(select 1 from table1 b where b.id=a.id)??我不是很懂给
      

  3.   

    表结构和题目是基本相同的
    update testaa,testcc
    set testaa.name1 = testcc.mingcheng
    where testcc.bianhao in (select distinct testcc.bianhao 
    from testaa,testcc
    where testcc.bianhao = testaa.id1)
    为什么会有错?报miss keyword?要怎么改?
      

  4.   

    1楼的回答很好。exists验证存在性,如果没有这个条件,当第二个表中的id在表1中找不到时,会被更新为空值
      

  5.   

    oracle不支持这样的写法改成
    update testaa
    set name1=(select mingcheng from testcc where rownum=1 and bianhao=testaa.id1)
    where exists(select 1 from testcc where bianhao=testaa.id1)