MySql update inner join!MySql跨表更新 多表update sql语句?如何将select出来的部分数据update到另一个表里面?

项目中,评论数,关注数等数据,是实时更新的。+1,-1 这种。

有的时候,可能统计不准确。

需要写一个统计工具,更新校准下。


用Java写SQL和函数,代码很清晰,方便扩展,但是太慢了。

为了简单起见,只写sql来统计,然后更新。(不想写存储过程)

语句如下:

#更新一个人的 关注数 followingCount

  1. update behavior_redman_count a  
  2. inner join  
  3. (  
  4. select memberId,count(*) as followingCount from behavior_follow where type = 10  
  5. and isDelete=0 group by memberId   
  6. )b   
  7. set a.followingCount =b.followingCount  
  8. where a.redmanId = b.memberId;  
update behavior_redman_count a
inner join
(
select memberId,count(*) as followingCount from behavior_follow where type = 10
and isDelete=0 group by memberId 
)b 
set a.followingCount =b.followingCount
where a.redmanId = b.memberId;




#MySQL不支持这种语法
  1. update behavior_redman_count  
  2. set followingCount = b.followingCount  
  3. from  
  4. (  
  5. select count(*) as followingCount from behavior_follow where type = 10 and memberId = 198  
  6. and isDelete=0  
  7. )b   
  8. where redmandId = a.memberId and a.memberId= 198;  
update behavior_redman_count
set followingCount = b.followingCount
from
(
select count(*) as followingCount from behavior_follow where type = 10 and memberId = 198
and isDelete=0
)b 
where redmandId = a.memberId and a.memberId= 198;



参考资料

1. Mysql跨表更新 多表update sql语句总结

http://www.jb51.net/article/32648.htm


2.如何将select出来的部分数据update到另一个表里面?

http://bbs.csdn.net/topics/320233666

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值