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

querysbja 2012-02-11 08:35:21
我对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的大侠帮忙看看究竟是哪里的问题,比较急,谢谢各位了!!


...全文
76 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
紫竹林畔 2012-02-12
  • 打赏
  • 举报
回复
querysbja 2012-02-11
  • 打赏
  • 举报
回复
明天我去试试,谢谢各位了
紫竹林畔 2012-02-11
  • 打赏
  • 举报
回复
关于这部分原因
楼主搜下
NULL的三值逻辑 变知道其中的缘由了
紫竹林畔 2012-02-11
  • 打赏
  • 举报
回复
改成not exists什么事都解决了
叶子 2012-02-11
  • 打赏
  • 举报
回复

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
*/
叶子 2012-02-11
  • 打赏
  • 举报
回复

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)
*/

原因是A表中有null值。
紫竹林畔 2012-02-11
  • 打赏
  • 举报
回复
--try
select *
from b t
where not exists(select 1 from a where empno=t.empno)

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧