问题
I'm trying to Select all the records in my database that don't exist in a subquery.
For some reason it returns nothing even though the sub query returns 2000 or so rows on it's own and the main query returns over 5000. I need all the records that aren't contained in the subquery
SELECT ID
FROM PART
WHERE NOT ID IN
(
SELECT DOCUMENT_ID AS ID
FROM USER_DEF_FIELDS
WHERE PROGRAM_ID = 'VMPRTMNT' AND ID = 'UDF-0000029'
)
回答1:
This is better written as a correlated NOT EXISTS subquery.
SELECT ID
FROM PART
WHERE NOT EXISTS
(
SELECT 1
FROM USER_DEF_FIELDS
WHERE PROGRAM_ID = 'VMPRTMNT'
AND ID = 'UDF-0000029'
AND DOCUMENT_ID = PART.ID
)
来源:https://stackoverflow.com/questions/30923125/sql-select-where-not-in-select-statement