有两个表,
表a: (aid, a_active)表b: (bid, aid, b_active)active 取值 0, 1 (true, false)。 表a中 a_active的值取决于 表b中 b_active的值, 当 a.aid=b.aid, b.b_active全为0 时, a的a_active 为 0 , 否则为 1想要更新 表a中的a_active的值, sql语句如何实现这个更新???不知道我的描述大家能不能看明白,希望能得到高手的指点。谢谢!

解决方案 »

  1.   

    update a 
    set a_active=(select case when b.b_active=0 then a.a_active=0 else 1 end from a,b where a.aid=b.aid)  
    where a.aid=b.aid and b.b_active=0
      

  2.   

    update a  
    set a_active=(select case when b.b_active=0 then a.a_active=0 else 1 end) from a,b where a.aid=b.aid)   
    where a.aid=b.aid and b.b_active=0 
      

  3.   

    update a
    set a_active=(select max(b_active) from b where aid=a.aid)
      

  4.   

    谢谢ACMIN_CHM和lzd_83的帮助,我试了一下,有语法错误,我用了一个比较复杂的方法去实现了:update a, (select aid, max(active) as c from b group aid) as tb_tmp
    set a.a_active=tb_tmp.c 
    whre a.aid=tb_tmp.aid