以下查询选择输入“完成”的所有工作对象(第7行的子查询)。当只有一个输入时,这可以正常工作。我该如何更改它,以便它要求每个输入都在子查询返回的输入中,而不仅仅是出现的输入之一?

SELECT workunits.ID
FROM workunits
LEFT JOIN workunitInputs ON workunits.ID = workunitInputs.workunitID
WHERE workunits.ID NOT IN (SELECT workunitID FROM jobworkunitassoc)
AND (
    workunitInputs.inputID IN (
        SELECT workunitOutputs.outputID
        FROM workunitOutputs
        LEFT JOIN workunits ON workunitOutputs.workunitID = workunits.ID
        LEFT JOIN jobworkunitassoc ON workunits.ID = jobworkunitassoc.workunitID
        LEFT JOIN jobs ON jobworkunitassoc.jobID = jobs.ID
        WHERE jobs.done = 1
    )
    OR workunitInputs.inputID IS NULL
)
GROUP BY workunits.ID


谢谢,伊斯特万。

最佳答案

将该子句更改为:

AND (
    workunitInputs.inputID NOT IN (
        SELECT workunitOutputs.outputID
        FROM workunitOutputs
        LEFT JOIN workunits ON workunitOutputs.workunitID = workunits.ID
        LEFT JOIN jobworkunitassoc ON workunits.ID = jobworkunitassoc.workunitID
        LEFT JOIN jobs ON jobworkunitassoc.jobID = jobs.ID
        WHERE jobs.done != 1
    )


这基于逻辑重言式:Y中的所有X ===!Y中的X

09-27 20:06