如何将嵌套的sql查询转换为join?

时间:2017-09-29 09:49:06

标签: sql join nested db2

请帮助我使用join重写以下查询: -

update "+tableName+" set F_RK='T' 
where ISN in ( 
    SELECT DISTINCT B.ISN 
    FROM "+tableName+"_PE_BA AS B,"+regisTableName+" AS C, F0093 AS D  
    WHERE B.F_BB = C.F_AB
    AND C.F_FB = D.F_AB
    AND D.F_OA = 1 
    AND C.F_FC = (SELECT MAX(E.F_FC) FROM "+regisTableName+" AS E WHERE E.F_AB = C.F_AB)) 
and F_RK is null or F_RK=''";

另外,我想优化查询。

1 个答案:

答案 0 :(得分:1)

要进行优化,请考虑where exists(可能会也可能不会提高效果)。为了便于阅读,请使用显式连接。

Update MyTable
set F_RK = 'T'
where exists
(
select 1
from Table2 B
inner join Table3 C
  on B.F_BB = C.F_AB
inner join Table3 D
  on C.F_FB = D.F_AB
  AND D.F_OA = 1 
where C.F_FC = (SELECT MAX(E.F_FC) FROM Table4 E WHERE E.F_AB = C.F_AB)
)
or F_RK is null 
or F_RK=''