EXISTS命令測試子查詢中是否存在任何記錄,如果子查詢返回一個或多個記錄,則返回true。
以下SQL列出了產(chǎn)品價格低于20的供應(yīng)商:
SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products WHERE SupplierId = Suppliers.supplierId AND Price < 20);
以下SQL列出了產(chǎn)品價格等于22的供應(yīng)商:
SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products WHERE SupplierId = Suppliers.supplierId AND Price = 22);