将IN子查询重写为连接

时间:2013-08-20 20:03:22

标签: sql join subquery

有人可以帮帮我吗?有没有办法将以下IN子查询重写为连接?非常感谢任何帮助。

SELECT DISTINCT 
    p.processID
    ,p.processName
    ,p.highLevelOrder
FROM 
    tblProcess p
WHERE
    p.highLevel = 1 and processID 
    in (select processID from tblJobCPR, tblJobTracking where tblJobCPR.cprID = tblJobTracking.cprID and tblJobTracking.effectiveEnd >= @processDate)
ORDER BY 
    p.highLevelOrder

2 个答案:

答案 0 :(得分:1)

这应该做:

SELECT DISTINCT 
    p.processID
    ,p.processName
    ,p.highLevelOrder
FROM 
    tblProcess p
INNER JOIN tblJobCPR jc
    ON p.processID = jc.processID
INNER JOIN tblJobTracking jt
    ON jc.cprID = jt.cprID
WHERE
    p.highLevel = 1 
    AND jt.effectiveEnd >= @processDate 
ORDER BY 
    p.highLevelOrder

答案 1 :(得分:0)

如果列processIDtblJobCPR表中而不是尝试这种方式:

SELECT DISTINCT 
    p.processID
    ,p.processName
    ,p.highLevelOrder
FROM 
    tblProcess p
JOIN tblJobCPR j       on  j.processID = p.processID
JOIN tblJobTracking jt on  j.cprID = jt.cprID 
                       and jt.effectiveEnd >= @processDate
WHERE
    p.highLevel = 1 
ORDER BY 
    p.highLevelOrder