要更新A表的一个字段aa,这个表还有一个机构ID字段,
这个机构ID字段是分布在表X,表Y,表Z中(一个机构ID只能属于一个表),表X,表Y,表Z中有一个字段bb,
要在这三个表中找到机构ID对应的bb,把bb的值赋值到A表的aa字段中,请问怎么实现呢?多谢:)

解决方案 »

  1.   

    update a 
       set aa=
         (
           select z.bb
            from z
            where z.id=a.id
            union all
            select y.bb
            from y
            where y.id=a.id
            union all
            select x.bb
            from x
            where x.id=a.id
         )
    where exists
          (
           select 1
              from x
                 where x.id=a.id
           union all
           select 1
              from y
                 where y.id=a.id
           union all
           select 1
              from z
                 where z.id=a.id
           
           )
      

  2.   

    update a 
       set aa=
         (
           select z.bb
            from z
            where z.id=a.id
            union all
            select y.bb
            from y
            where y.id=a.id
            union all
            select x.bb
            from x
            where x.id=a.id
         )
    where exists
          (
           select 1
              from x
                 where x.id=a.id
           union all
           select 1
              from y
                 where y.id=a.id
           union all
           select 1
              from z
                 where z.id=a.id
           
           )