这看起来应该是一个相对简单的查询,但有人可以解释一下为什么第二个查询不返回第 2 行和第 3 行,但第三个查询却返回行?基本上,如何使查询满足两个“not in”子句?
declare @t table (id int identity, code1 char(2), code2 char(2))
insert into @t (code1, code2) values ('AA','BB')
insert into @t (code1, code2) values ('AA','CC')
insert into @t (code1, code2) values ('DD','EE')
select * from @t where code1 = 'AA' and code2 = 'BB'
select * from @t where (code1 != 'AA' and code2 != 'BB')
select * from @t t1 left join @t t2 on t2.id = t1.id and t2.code1 = 'AA' and t2.code2 = 'BB' where t2.id is null
* 已更新*
感谢您的回答。我希望“(code1 != 'AA' and code2 != 'BB')”都需要评估,即第1行(AA,BB)将被过滤掉,但rwo 2(AA,CC)将被过滤掉被退回。
是否没有简单的方法来规定需要满足两个约束,这就是我希望通过使用括号和 and 语句来实现的?
最佳答案
对于查询 2,第二行的 code1 != 'AA'
为 FALSE。
关于具有多个 'not in' 检查的 SQL where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4226495/