有两个表
a表
sp_id  the_num
1001   5
1002   10

b表
sp_id  num
1001   2
1002   1
1002   6

从a表加上b表中对应的数量,以sp_id对应,我用
update a set the_num=a.the_num + b.num from a,b where a.sp_id = b.sp_id
执行以后,对于a表中的1002,怎么只是10+1=11呢?而不是10+1+6=17.请求解。

解决方案 »

  1.   

    update a set the_num=a.the_num + b.num 
    from a,(select sp_id,sum(num) as num from b group by sp_id) as b
      where a.sp_id = b.sp_id
      

  2.   

    update
     a 
    set
     the_num=a.the_num + b.num 
    from
     a,
    (select sp_id,sum(num) as num from b group by sp_id) as b
    where 
     a.sp_id = b.sp_id
      

  3.   

    因为级联后的数据是这样的:
    sp_id  the_sum  sp_id  num
    1001  5  1001  2
    1002  10 1002  1
    1002  10 1002  6
    ...
    然后再按WHERE进行UPDATE,所以不会出现你想要的效果