场景:数据库表里的多个字段通过另外一张表的字段关联查询出来并update
常规逻辑sql
每个字段都分别取一下再一个个更新(速度慢,需要优化)
UPDATE table1 vf
set vf.CUSTOMER_TEL =
(select v2.客户联系人电话
from table2 v2
where vf.accountid = v2.客户账号
and rownum = 1),
vf.ROOM_NUMBER =
(select v2.房号
from table2 v2
where vf.accountid = v2.客户账号
and rownum = 1),
vf.BRANCH_OFFICE =
(select v2.分公司
from table2 v2
where vf.accountid = v2.客户账号
and rownum = 1 )
where vf.ifsplit = '1'
ORCALE
关联查询,整体入库(优化后)
UPDATE table1 vf0
set (vf0 .CUSTOMER_TEL, vf0 .ROOM_NUMBER, vf0 .BRANCH_OFFICE) =
(select v2.客户联系人电话, v2.房号, v2.分公司
from table1 vf
INNER JOIN table2 v2
ON vf.accountid = v2.客户账号 and rownum = 1 and vf0.id = vf.id)
where vf0.ifsplit = '1'
MYSQL
关联查询,整体入库(优化后)
UPDATE table1 vf
INNER JOIN table2 v2 ON vf.accountid = v2.客户账号 and rownum = 1 and vf0.id = vf.id
SET vf.CUSTOMER_TEL = v2.客户联系人电话,
vf.ROOM_NUMBER = v2.房号,
vf.BRANCH_OFFICE = v2.分公司
WHERE vf.ifsplit = '1'
网友评论