T-SQL“不在的地方"使用两列 [英] T-SQL "Where not in" using two columns
问题描述
我想从表 T1 中选择所有记录,其中 A 列和 B 列中的值与表 T2 中的 C 列和 D 列没有匹配的元组.
I want to select all records from a table T1 where the values in columns A and B has no matching tuple for the columns C and D in table T2.
在mysqlWhere not in"中使用两列我可以阅读如何使用表单 select A,B from T1 where (A,B) not in (SELECT C,D from T2) 来完成,但在 T-SQL 中失败,导致','附近的语法不正确.".
In mysql "Where not in" using two columns I can read how to accomplish that using the form select A,B from T1 where (A,B) not in (SELECT C,D from T2), but that fails in T-SQL for me resulting in "Incorrect syntax near ','.".
那我该怎么做?
推荐答案
使用相关子查询:
...
WHERE
NOT EXISTS (
SELECT * FROM SecondaryTable WHERE c = FirstTable.a AND d = FirstTable.b
)
确保在(c, d)
上的 SecondaryTable 上有一个复合索引,除非该表不包含很多行.
Make sure there's a composite index on SecondaryTable over (c, d)
, unless that table does not contain many rows.
这篇关于T-SQL“不在的地方"使用两列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!