[mysql]求个sql语句

jom_ch 2012-03-30 02:30:59
表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值不一致。

...全文
49 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
rucypli 2012-03-30
  • 打赏
  • 举报
回复
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
jom_ch 2012-03-30
  • 打赏
  • 举报
回复
嗯,不错

用在了 ucenter home

update 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
ACMAIN_CHM 2012-03-30
  • 打赏
  • 举报
回复
update A ,(select cid,count(*) as cnt from B group by cid) as t
set a.sum=b.cnt
where A.id=b.cid;
wwwwb 2012-03-30
  • 打赏
  • 举报
回复
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

56,675

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧