读书人

为什么没有数据呢?该怎么解决

发布时间: 2012-04-11 17:42:33 作者: rapoo

为什么没有数据呢?
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 @tselect 1 union allselect 2 union allselect 3 union allselect 4 union allselect nulldeclare @t1 table (col int)insert into @t1select 4 union allselect 5 union allselect 6 union allselect 7 union allselect nullselect * from @t1 where col not in (select * from @t) 

读书人网 >SQL Server

热点推荐