日期:2014-05-18  浏览次数:20539 次

急,怪异的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
--try
select *
from b t
where not exists(select 1 from a where empno=t.empno)

------解决方案--------------------
SQL code

declare @B table (empno int)
insert into @B
select 40001 union all
select 40002

select * from @B where empno='40001' --在B表是可以检索出数据的,
/*
empno
-----------
40001
*/

declare @A table (empno int)
insert into @A
select null union all
select 40003

select * 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 @B
select 40001 union all
select 40002

select * from @B where empno='40001' --在B表是可以检索出数据的,
/*
empno
-----------
40001
*/

declare @A table (empno int)
insert into @A
--select null union all
select 40003

select * 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
-----------
40001
40002
*/

------解决方案--------------------
改成not exists什么事都解决了
------解决方案--------------------
关于这部分原因
楼主搜下
NULL的三值逻辑 变知道其中的缘由了