update a set counts=newcounts
from a inner join
(select aid, count(1) as newcounts from b group by aid)
b
on a.id=b.aid 

解决方案 »

  1.   

    hlq8210 朋友,当统计结果为0的时候,A表的counts没有更新啊?
      

  2.   

    update a set counts=(select count(1) from b where a.id=b.aid )
    from a
      

  3.   

    谢谢,问题可解决!不过,为什么这样更新不是所有的counts值都一样的呢?
      

  4.   


    A[信息分组表]字段:id,counts[记录B表中Aid与id值相等的总数] 
    B[详细信息表]字段:id,Aid[Aid=A.id] update a set counts=b.counts 
    from a,(select Aid,count(1) as counts from b) b
    where a.aid=b.aid