两个表aaa和bbb表,当aaa表中的A字段和bbb表中的A字段,aaa表中的B字段和bbb表中的B字段相同时,则把aaa表中的ID更新到bbb表中的ID字段,用UPDATE语句要怎样写呢?

解决方案 »

  1.   

    UPDATE bbb x SET x.id = (SELECT y.id FROM aaa y WHERE x.a = y.a AND x.b = y.b)
    WHERE EXISTS (SELECT 1 FROM aaa y WHERE x.a = y.a AND x.b = y.b);
      

  2.   

    update aaa a
       set a.ID=b.ID from
                                   (select *
                            from bbb b
                           where (select count(*)
                                    from bbb
                                   where a.A = A
                                     and a.B =B)=1) b
                           where
                                     and a.A = A
                                     and a.B =B
    但是提示:SQL命令未正确结束
      

  3.   

    update bbb set (col1,col2,col3...)=(select col1,col2,col3... from aaa
      where a=bbb.a and b=bbb.b)
    where exists(select 1 from aaa where a=bbb.a and b=bbb.b)
      

  4.   


    oracle好像没有update...from语句
      

  5.   

    update testb  set id=(select a1.id from testa a1, testb b1 where a1.a=b1.a and a1.b=b1.b);
      

  6.   

     这种需求可以用merege来写啊:
       MERGE INTO aaa
     USING (SELECT * FROM bbb WHERE aaa.a = bbb.a) c
     ON aaa.b = c.b
     WHEN MATCHED THEN
       UPDATE SET id = c.id;