这个语句为什么查不出来

dairy1 2006-07-20 12:51:09
select * from h_xzmx where bdhm ='000000187893'
查到无该纪录

select * from temptable where bdhm ='000000187893'
查到有该纪录,找到一条


select * from temptable where bdhm not in (select bdhm from h_xzmx)
无纪录。

问题:h_xzmx是个工资集合,有数千条保单号码,TEMPTABLE是一个业务员的临时表,该表中有的保单号bdhm会出现在h_xzmx中,有的不出现。另两个表中都存在少数NULL值
那么例题中的保单号,既然工资中没有,临时表中有。那我用第三句语句查哪些在临时表中有的,但在工资表中没有的,结果一条也没找到,那与上述单项查询逻辑不符啊,用TRIM等办法我都试过了,仍然没用。
...全文
244 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
gahade 2006-07-20
  • 打赏
  • 举报
回复
declare @a varchar(100)
create table #h_xzmx(bdhm varchar(100))
insert #h_xzmx
select '1111'
union all select @a

create table #temptable(bdhm varchar(100))
insert #temptable
select '000000187893'
union all select @a

select bdhm from #h_xzmx
--有二条

select * from #temptable
--有二条

select * from #temptable where bdhm not in (select bdhm from #h_xzmx)
--无

select * from #temptable where bdhm not in (select isnull(bdhm,'') from #h_xzmx)
--有一条

select * from #temptable where bdhm not in (select bdhm from #h_xzmx where bdhm is not null)
--有一条

drop table #h_xzmx
drop table #temptable

分析结果.not in 子句受空值影响
marco08 2006-07-20
  • 打赏
  • 举报
回复
学习...
dairy1 2006-07-20
  • 打赏
  • 举报
回复
我知道了,要在后面去除NULL值

select * from temptable where bdhm not in (select bdhm from h_xzmx where bdhm is not null)
这样才行,你们知道为什么吗,我搞不明白
hellowork 2006-07-20
  • 打赏
  • 举报
回复
向h_xzmx表中插入一bdhm ='000000187893'的新行,然后在查询一下.如果成功则说明h_xzmx表中bdhm列的值输入有问题,可能有前空格,类似' 000000187893'.
dairy1 2006-07-20
  • 打赏
  • 举报
回复
奇怪,我这儿就这样的
gahade 2006-07-20
  • 打赏
  • 举报
回复
语句没有错误啊.我自己试的也不会.

create table #h_xzmx(bdhm varchar(100))
create table #temptable(bdhm varchar(100))
insert #temptable
select '000000187893'

select * from #h_xzmx where bdhm ='000000187893'
--没有记录

select * from #temptable where bdhm ='000000187893'
--有一条

select * from #temptable where bdhm not in (select bdhm from #h_xzmx)
--有一条

drop table #h_xzmx
drop table #temptable

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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