EXISTS命令测试子查询中是否存在任何记录,如果子查询返回一个或多个记录,则返回true。
以下SQL列出了产品价格低于20的供应商:
SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products WHERE SupplierId = Suppliers.supplierId AND Price < 20);
以下SQL列出了产品价格等于22的供应商:
SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products WHERE SupplierId = Suppliers.supplierId AND Price = 22);