表 1  id  jh  flag
1   A    0
2   B    0表2cid  cjh flag1     A1  1
1     A2  1
表1的ID 就是表2 的CID 当表2 的cid 的flag都是1时 表1 的第一行 flag 也是1  

解决方案 »

  1.   

    select A.id,A.jh,case when n=0 then 0 else 1 end as flag
    from
    (
    select A.id,A.jh,min(flag) n
    from tb1 A,tb2 B
    where A.id = B.cid
    group by A.id,A.jh
    )
      

  2.   

    select id, jh, flag = case when not exists (select 1 from 表2 where cid=t.id and flag <> 1) then 1 else 0 end from 表1 t
      

  3.   

    --写个更新的
    update tb1
    set flag=1
    from tb1 join tb2 on tb1.id=tb2.cid
    where not exists(select * from tb2 where tb1.id=cid and flag<>1)
      

  4.   

    是表2update时候改表1?
    用trigger