sql中如何 将一张表的数据 更新到另外一张表的字段中

今天操作数据库       有个需求需要从一张流水表中有用户付款金额    还有一张  是用户的结算金额    每个用户的付款和结算费率和手续费等都不一样   需要将剩余可以结算的金额查询出来  进行更新到各自对应的用户后面  花了几分钟写出来去测试了一下  没问题  

万变不离其中   依然还是  在 update  +表(及其关联表) +set  +更改字段 +where 条件

update merchant m join (
select pc.cp_channel channelId,0.01*sum(pc.real_pay) as totalmoney, scs.flow ,(0.01*sum(pc.real_pay))-flow as settlementmoney
from pay_record pc
right join
(select channalId,sum((money+charge)/(1-rate)) as flow from settlement where status > -1 GROUP BY channalId)
scs on scs.channalId=pc.cp_channel GROUP BY pc.cp_channel
)a on a.channelId=m.channel_id

set m.money= a.settlementmoney where a.channelId=m.channel_id

猜你喜欢

转载自www.cnblogs.com/mrxiab/p/10595507.html