update b set kc=kc-a.cn from a,b where b.aid=a.bid and a.orderid='6'

解决方案 »

  1.   

    update b
    set kc = kc -isnull((select sum(cn) from a where aid = bid and orderid = 6),0)
      

  2.   

    update b
    set kc = kc -isnull((select sum(cn) from a where aid = b.bid and orderid = 6),0)
      

  3.   

    create table a(aid int,cn int,orderid int)
    insert into a
    select 1,2,6 union all
    select 2,1,6 union all
    select 3,4,6 union all
    select 4,5,7create table b(bid int,kc int)
    insert into b
    select 1,200 union all
    select 2,200 union all
    select 3,200 union all
    select 4,200update b set kc = kc - a.cn from a where a.aid = b.bid and a.orderid = 6
    select * from a
    select * from b
    /*
    (所影响的行数为 3 行)aid         cn          orderid     
    ----------- ----------- ----------- 
    1           2           6
    2           1           6
    3           4           6
    4           5           7(所影响的行数为 4 行)bid         kc          
    ----------- ----------- 
    1           198
    2           199
    3           196
    4           200(所影响的行数为 4 行)
    */
    drop table a
    drop table b
      

  4.   

    update b set kc=kc -isnull(c.num,0) from b,(select aid, sum(cn)as num from a group by aid where orderid = 6) c where b. bid =c.aid
      

  5.   

    update b表 set
    kc=kc-(select kc from a,b
    where 订单id=6 and a.id=b.id)
      

  6.   

    update b set kc=kc+a.cn from a where a.aid=b.bid and orderid=6