mysql如何实现先查询后更新的sql语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql如何实现先查询后更新的sql语句相关的知识,希望对你有一定的参考价值。
使用一条sql,将查询出来的结果如何批量更新到另外一张表
参考技术A UPDATE men_info as a INNER JOIN (select men_id,sum(amount) as amount from men_recharge_info where busin_type =1 and is_refund=0 and recharge_state=3 group by men_id) as b ON a.id=b.men_id SET a.total_recharge = b.amount; 参考技术B 使用update进行更新追问UPDATE men_id t
INNER JOIN (SELECT men_id,SUM(amount) AS temp FROM men_recharge_info GROUP BY men_id) as m ON m.men_id = t.men_id
SET t.total_recharge = m.temp
这条语句行不行?
应该是可以的,你也可以试试下面的语句,因为没有你那边的表结构,我写的可能会有点问题需要调整
update men_id t set t.SET t.total_recharge = n.temp
where t.men_id in
(SELECT
m.men_id,
SUM(m.amount) AS temp
FROM men_recharge_info m
GROUP BY men_id) n
以上是关于mysql如何实现先查询后更新的sql语句的主要内容,如果未能解决你的问题,请参考以下文章