Any*_*are 1 sql sql-server sql-server-2012
我尝试执行以下查询,但它不会获得任何数据,虽然它应该得到一行:
select * from [DB1.table1] where col1 not in (select col2 from DB2.table2)
Run Code Online (Sandbox Code Playgroud)
col1,col2的类型为varchar
为什么它不起作用?
" 不起作用 "并不是对您的问题的完美描述,但几乎在所有情况下,这都是由子选择返回NULL值引起的.
你可能想要这个:
select * from [DB1.table1]
where col1 not in (select col2 from DB2.table2 where col2 is not null);
Run Code Online (Sandbox Code Playgroud)
与NULLalways 的比较总是产生"undefined",因此如果子选择中的至少一行NULL在col2列中包含a ,则整个表达式是"未定义的".由于undefined不是"true",整个查询不会返回任何内容.