开发者

How do I get all the results not in a set? [closed]

开发者 https://www.devze.com 2023-04-13 05:20 出处:网络
It's difficult to tell what is 开发者_开发技巧being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form.
It's difficult to tell what is 开发者_开发技巧being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

Im joining two tables and I want to then join a third but I want to get the result of the records that dont join. Dont really know what to use to do this. Can I search for nulls that appear outside the join or something?


The best-performing way to do this in general is to use NOT IN or NOT EXISTS - they are identical behind the scenes in SQL Server 2005+.

They are preferred over LEFT JOIN...IS NULL because they short circuit - as soon as the matching condition is found, that record is skipped. LEFT JOIN loads the whole data set and relation, then eliminates records afterwards.

SELECT a.*
FROM TableA a
<other joins>
WHERE a.ComparisonField NOT IN (SELECT RelationField FROM OtherTable)

or

SELECT a.*
FROM TableA a
<other joins>
WHERE NOT EXISTS(SELECT 1 
                 FROM OtherTable o
                 WHERE o.Relationfield = a.Comparisonfield)
0

精彩评论

暂无评论...
验证码 换一张
取 消