当前位置: 代码迷 >> Sql Server >> 急怪异的SQL有关问题,各位大侠帮忙看下
  详细解决方案

急怪异的SQL有关问题,各位大侠帮忙看下

热度:41   发布时间:2016-04-27 14:47:35.0
急,怪异的SQL问题,各位大侠帮忙看下
我对SQLSERVER2005数据库中2个表进行操作
具体的SQL语句是:
select * from B where empno='40001'在B表是可以检索出数据的,
select * from A where empno='40001'在A表中查不出任何数据
但是如果我下面的语句又一条数据都查不出来
select * from B where empno not in (select empno from A) 
其中A表中empno字段和B表中的empno字段是相同的

各位精通SQL的大侠帮忙看看究竟是哪里的问题,比较急,谢谢各位了!!




------解决方案--------------------
SQL code
--tryselect *from b twhere not exists(select 1 from a where empno=t.empno)
------解决方案--------------------
SQL code
declare @B table (empno int)insert into @Bselect 40001 union allselect 40002select * from @B where empno='40001' --在B表是可以检索出数据的,/*empno-----------40001*/declare @A table (empno int)insert into @Aselect null union allselect 40003select * from @A where empno='40001'--在A表中查不出任何数据/*empno-----------(0 row(s) affected)*/select * from @B where empno not in (select empno from @A)  /*empno-----------(0 row(s) affected)*/
------解决方案--------------------
SQL code
declare @B table (empno int)insert into @Bselect 40001 union allselect 40002select * from @B where empno='40001' --在B表是可以检索出数据的,/*empno-----------40001*/declare @A table (empno int)insert into @A--select null union allselect 40003select * from @A where empno='40001'--在A表中查不出任何数据/*empno-----------(0 row(s) affected)*/--把A表中的null去掉,结果就有了select * from @B where empno not in (select empno from @A)  /*empno-----------4000140002*/
------解决方案--------------------
改成not exists什么事都解决了
------解决方案--------------------
关于这部分原因
楼主搜下
NULL的三值逻辑 变知道其中的缘由了
  相关解决方案