为什么没有数据呢?

风中云 2012-01-13 09:53:47
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型不能这样查吗?希望大家都指点指点,谢谢了......
...全文
83 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
大力水手 2012-01-13
  • 打赏
  • 举报
回复

select * from SM_Received_SM_List where pad3 not in (select userappend from mdao_sys_account WHERE userappend IS NOT NULL)
或者直接用not exists == not in and 字段 is not null
唐诗三百首 2012-01-13
  • 打赏
  • 举报
回复
我猜应该是真的没数据,

SM_Received_SM_List的所有pad3, 都在mdao_sys_account的userappend里.
风中云 2012-01-13
  • 打赏
  • 举报
回复
谢谢各位大侠与前辈的帮助,问题解决了,谢谢了
风中云 2012-01-13
  • 打赏
  • 举报
回复
在mdao_sys_account表中的userappend(唯一)只有三个值:NULL,2,3;
SM_Received_SM_List表中的pad3中值有多个:007,123123,2在查询中应该把pad3值为007和123123的值查出来的,但是结果还是0条记录
叶子 2012-01-13
  • 打赏
  • 举报
回复
因为有null。


--这样你是得不到结果的
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)
SQL77 2012-01-13
  • 打赏
  • 举报
回复
select * from SM_Received_SM_List where pad3 not in (select userappend from mdao_sys_account WHERE userappend IS NOT NULL)
--小F-- 2012-01-13
  • 打赏
  • 举报
回复
去左右两边空格
风中云 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ap0405140 的回复:]
try this,

SQL code


select *
from SM_Received_SM_List
where ltrim(rtrim(pad3))
not in
(select ltrim(rtrim(userappend)) from mdao_sys_account)
[/Quote]

试了下,还是没有记录?能解释一下ltrim(rtrim(pad3))
吗,没用过
唐诗三百首 2012-01-13
  • 打赏
  • 举报
回复
try this,

select *
from SM_Received_SM_List
where ltrim(rtrim(pad3))
not in
(select ltrim(rtrim(userappend)) from mdao_sys_account)
风中云 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sql77 的回复:]
可能用两种情况
select userappend from mdao_sys_account WHERE userappend IS NULL
存在

要不然就是确实没有数据
[/Quote]
这个对查询有影响吗,应该怎么改?
风中云 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sql77 的回复:]
可能用两种情况
select userappend from mdao_sys_account WHERE userappend IS NULL
存在

要不然就是确实没有数据
[/Quote]
是啊,userappend确实有一条记录的值是NULL
SQL77 2012-01-13
  • 打赏
  • 举报
回复
可能用两种情况
select userappend from mdao_sys_account WHERE userappend IS NULL
存在

要不然就是确实没有数据

22,207

社区成员

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

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