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

为什么没有数据呢?
1、select * from SM_Sent_SM_List where sysaccountid not in (select serialno from mdao_sys_account)
2、select * from SM_Received_SM_List where pad3 not in (select userappend from mdao_sys_account)
上面是两个子查询,其中SM_Sent_SM_List表中的sysaccountid 引用mdao_sys_account表中的serialno 字段,这两个字段都是int型的,但是它们之间没有建立主外键关系,查询条件就是当SM_Sent_SM_List 表sysaccountid字段中的值不能在mdao_sys_account表的serialno字段找到相等的值时候查询SM_Sent_SM_List 表的记录,这个查询没有问题;
第二个查询和第一个条件类似,其中SM_Received_SM_List表中的pad3 引用mdao_sys_account表中的userappend 字段,这两个字段都是varchar型的,它们之间也没有建立主外键关系,也是查询当SM_Received_SM_List 表中pad3 字段值不能在mdao_sys_account表的userappend字段找到相等的值时候查询SM_Received_SM_List 表的记录,但是为什么查询结果总是0条记录呢,SM_Received_SM_List 表中存在符合查询条件的数据的
有知道的吗?帮忙指点一下,这两个查询条件都是类似的,为什么第二个查不到记录呢,它们之间唯一的不同就是数据类型了,一个是int型一个是varchar型,难道varchar型不能这样查吗?希望大家都指点指点,谢谢了......


------解决方案--------------------
try this,
SQL code

select * 
from SM_Received_SM_List 
where ltrim(rtrim(pad3)) 
not in 
(select ltrim(rtrim(userappend)) from mdao_sys_account)

------解决方案--------------------
去左右两边空格
------解决方案--------------------
select * from SM_Received_SM_List where pad3 not in (select userappend from mdao_sys_account WHERE userappend IS NOT NULL)
------解决方案--------------------
因为有null。

SQL code

--这样你是得不到结果的
declare @t table (col int)
insert into @t
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select null

declare @t1 table (col int)
insert into @t1
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select null

select * from @t1 where col not in (select * from @t)