多表联合操作

有如下三张表:
fund_forsale

fundCode(PRIMARY KEY)market_value
000706NULL
000707NULL
000708NULL

fund_gen_info

fund_codefund_id
0007061
0007072
0007083
0007094

fund_asset_conf

fund_idasset_valueenddate
10.1212345678
10.1312345679
10.0112345673
20.2312345675
20.2512345673
30.3112345671
40.4112345670

表fund_forsale更新方式为:选取表fund_asset_conf中对应最新(enddate倒序)基金的asset_conf字段

有以下两种方法

insert into fund_forsale (fundCode,market_value)
( select fund_code as fundCode,asset_nav as market_value
from (select b.fund_code,conf.* 
from fund_asset_conf conf,fund_gen_info b
where conf.fund_id=b.fund_id and b.fund_code in
(select fundCode from fund_forsale) 
order by enddate desc ) a
group by fund_code )
on duplicate key update market_value=values(market_value);

运行时间6.97s

update fund_forsale as c 
inner join 
(select fund_code, asset_nav 
from fund_gen_info natural join 
(select fund_id, asset_nav 
from fund_asset_conf natural join 
(select fund_id, max(enddate) as enddate 
from fund_asset_conf 
group by fund_id) as t1) 
as t2) b 
on b.fund_code=c.fundCode 
set c.market_value=b.asset_nav;

运行时间0.15s

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值