谁能告诉我为什么这两个查询返回的行数不一样?
SELECT ProductSubcategoryID AS ProdSubID
FROM Production.Product
WHERE EXISTS (
SELECT 1
FROM Production.Product
WHERE Weight IS NOT NULL
)SELECT ProductSubcategoryID AS ProdSubID
FROM Production.Product
WHERE Weight IS NOT NULL可以在AdventureWorks中找到Production.Product
发布于 2018-07-24 06:01:35
这些谓词与where子句中的谓词不同。
第一个查询将返回Production.Product中的每个ProductSubcategoryID,只要至少有一条记录的Weight不是null。
虽然第二个查询将返回同一行中Production.Product中的每个ProductSubcategoryID,但Weight不是null。
为了使查询等价,第一个查询中的子查询必须与外部查询相关-您可以这样做:
SELECT ProductSubcategoryID AS ProdSubID
FROM Production.Product As p0
WHERE EXISTS (
SELECT 1
FROM Production.Product As p1
WHERE p1.<row identifier> = p0.<row identifier>
AND Weight IS NOT NULL )其中,<row identifier>表示对于表中的每一行都是唯一的一个值(或一组值)。
https://stackoverflow.com/questions/51487732
复制相似问题