當您使用EXISTS
指定的子查詢本身內的約束。如果子查詢爲給定條件C.CUSTNUM = P.CNUM
返回至少一行,則認爲條件滿足。
我想這可以重寫一個JOIN
到PRODUCTS
表。
SELECT C.CUSTNUM
FROM CUSTOMERS AS C
WHERE EXISTS (
SELECT 1
FROM PURCHASES AS P
JOIN PRODUCTS AS PR ON
ON P.PNUM = PR.PNUM -- not sure what column to join on in PRODUCTS
WHERE
C.CUSTNUM = P.CNUM -- added the correlation, I believe this is what you mean
PR.MSRP = 9.99
)
如果不是這種情況,只要堅持到您的修改後的查詢:
SELECT C.CUSTNUM
FROM CUSTOMERS AS C
WHERE EXISTS (
SELECT 1
FROM PURCHASES AS P
WHERE
EXISTS (
SELECT 1
FROM PRODUCTS AS PR
WHERE PR.MSRP = 9.99
)
AND C.CUSTNUM = P.CNUM
但要注意的是,外部子查詢將從PURCHASES
返回所有行,如果有至少一個排PRODUCTS
表無論任何列的匹配如何(您沒有指定它們之間的任何相關條件)都有MSRP = 9.99
。
哪個RDBMS是這個呢?請添加一個標籤來指定您是使用'mysql','postgresql','sql-server','oracle'還是'db2' - 或者其他的東西。 –