SELECT
lpn.TC_LPN_ID
FROM LPN lpn
WHERE lpn.inbound_Outbound_Indicator = 'O'
AND lpn.tc_company_id = 1403
AND lpn.TC_REFERENCE_LPN_ID **IN**
(SELECT
l.tc_lpn_id
FROM lpn l
WHERE l.TC_COMPANY_ID = 1403
AND l.inbound_Outbound_Indicator = 'I'
AND (l.tc_lpn_id= '000990950' OR l.TC_REFERENCE_LPN_ID = '000990950'))
想要将我的查询修改为 EXISTS 。
EXISTS 提高效果。
答案 0 :(得分:1)
SELECT lpn.tc_lpn_id
FROM lpn lpn
WHERE lpn.inbound_outbound_indicator = 'O'
AND lpn.tc_company_id = 1403
AND EXISTS (
SELECT 1
FROM lpn l
WHERE lpn.tc_reference_lpn_id = l.tc_lpn_id
AND l.tc_company_id = 1403
AND l.inbound_outbound_indicator = 'I'
AND ( l.tc_lpn_id = '000990950'
OR l.tc_reference_lpn_id = '000990950'
))
答案 1 :(得分:0)
JOIN可以提高性能。
您可以将查询表达为自身的标准内部联接:
SELECT
lpn.TC_LPN_ID
FROM LPN
JOIN lpn l
ON l.TC_COMPANY_ID = lpn.l.TC_COMPANY_ID
AND l.tc_lpn_id = lpn.TC_REFERENCE_LPN_ID
AND l.inbound_Outbound_Indicator = 'I'
AND l.tc_lpn_id = '000990950' OR l.TC_REFERENCE_LPN_ID = '000990950')
WHERE lpn.inbound_Outbound_Indicator = 'O'
AND lpn.tc_company_id = 1403
请注意,我删除了lpn
对其自身的无意义别名 - 即FROM LPN lpn
与简单FROM LPN
相同:将表别名化为自己的名称无效。