表A
cid sum
1   100
2   105
3   200
...
======================
表B
id cid
1  1
2  1
3  1
...
表A的sum字段是表B的cid对应的汇总数,因为某些原因表B的按cid汇总数与表A的sum数不一致,现在我想用一个sql语句更新表A的sum字段,请问怎么实现?// 更清楚的说明
select count(*) as cnt from 表B where cid=1,结果cnt与表A的sum值不一致。

解决方案 »

  1.   

    update a inner join (select cid,count(*) as ca from b group by  cid) b1
    on a.id=b1.cid
    set a.sum=b1.ca
      

  2.   

    update A ,(select cid,count(*) as cnt from B group by cid) as t
    set a.sum=b.cnt
    where A.id=b.cid;
      

  3.   

    嗯,不错用在了 ucenter homeupdate uchome_mtag m inner join(select tagid,count(*) as cnt from uchome_thread group by tagid) t on m.tagid=t.tagid set m.threadnum=t.cnt
      

  4.   

    update a inner join (select cid,count(*) as ca from b group by cid) b1
    on a.id=b1.cid
    set a.sum=b1.ca