我試圖修改此Transact SQL語句以使用NOT EXISTS
子句而不是NOT IN
。我使用了搜索引擎,而且我很難應用我找到的例子。Transact SQL - 使用NOT EXISTS子句而不是NOT IN
SELECT StudentId
FROM Students
WHERE School NOT IN (SELECT School FROM ClosedSchools)
我試圖修改此Transact SQL語句以使用NOT EXISTS
子句而不是NOT IN
。我使用了搜索引擎,而且我很難應用我找到的例子。Transact SQL - 使用NOT EXISTS子句而不是NOT IN
SELECT StudentId
FROM Students
WHERE School NOT IN (SELECT School FROM ClosedSchools)
如果同時Students
和ClosedSchools
有SchoolId
場:
SELECT StudentId
FROM Students stu
WHERE NOT EXISTS (
SELECT School
FROM ClosedSchools clo
WHERE clo.SchoolId = stu.SchoolId);
SELECT StudentId
FROM Students AS a
WHERE NOT EXISTS (SELECT School
FROM ClosedSchools AS b
WHERE a.School = b.School)
SELECT s.StudentId
FROM Students s
WHERE NOT EXISTS (SELECT 1
FROM ClosedSchools
where ClosedSchools.School = s.School
)
或
SELECT s.StudentId
FROM Students s
LEFT JOIN ClosedSchools
on ClosedSchools.School = s.School
WHERE ClosedSchools.School is null;