我要吐血的问题,一个where引发的核战争

yunhaiC QQ654777694 2008-12-04 09:35:25
比如 where id=3
可以查出相关资料
但是 where id = 4就查不出来
有资料我可以保证
...全文
252 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 clq271520093 的回复:]
引用 26 楼 zjcxc 的回复:
你用 like '%775%'
如果这样能出来, 说明你的列中有特殊字符

老大,'%775%' 是什么意思咧?
[/Quote]


where 1= 1 
and main.diff_date >= '20081101' and main.diff_date <= '20081202'
and main.machine_id LIKE '%775%'
and dtl.adflag = 2
junying2yu 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 yunhaiC 的帖子:]
比如 where id=3
可以查出相关资料
但是 where id = 4就查不出来
有资料我可以保证
[/Quote]


你写成where id = 3试试
lanzhengwu 2008-12-04
  • 打赏
  • 举报
回复
我吐血..
三下鱼 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 zjcxc 的回复:]
你用 like '%775%'
如果这样能出来, 说明你的列中有特殊字符
[/Quote]
老大,'%775%' 是什么意思咧?
-狙击手- 2008-12-04
  • 打赏
  • 举报
回复
like '%775%'
fcuandy 2008-12-04
  • 打赏
  • 举报
回复
拜神
zjcxc 2008-12-04
  • 打赏
  • 举报
回复
你用 like '%775%'
如果这样能出来, 说明你的列中有特殊字符
butchroller 2008-12-04
  • 打赏
  • 举报
回复
虽然中途我也join了一些其他表
-----------------
用left outer join.
flairsky 2008-12-04
  • 打赏
  • 举报
回复
强制转换一下,顺便去下空格,应该没问题
昵称被占用了 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yunhaiC 的回复:]
where 1= 1 and main.diff_date >= '20081101' and main.diff_date <= '20081202' and main.machine_id = '775' and dtl.adflag = 2

这里的machine_id = '775'就查不出来,其他的id可以查出来,怎么会有这种问题
[/Quote]

machine_id = '775'
改成
ltrim(rtrim(machine_id)) = '775'
等不到来世 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 yunhaiC 的回复:]
我发觉在不填machine_id的情况下,查出的资料不含我需要的machine_id,所以加上这个where条件以后查不出来

但是很奇怪,我觉得主表跟这个machine表有关系吧,虽然中途我也join了一些其他表,但是就是没资料
我如果去掉其他表的join,那样就可以查出我需要的machine_id了
[/Quote]
hehe,早猜到是这样……

因为不太正确的联表导致记录为空。
你可以一个一个的表逐个join,看看是联表的哪个环节出错。
水族杰纶 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 yunhaiC 的回复:]
我发觉在不填machine_id的情况下,查出的资料不含我需要的machine_id,所以加上这个where条件以后查不出来

但是很奇怪,我觉得主表跟这个machine表有关系吧,虽然中途我也join了一些其他表,但是就是没资料
我如果去掉其他表的join,那样就可以查出我需要的machine_id了
[/Quote]
where 1= 1 and main.diff_date >= '20081101'  and main.diff_date <= '20081202' and rtrim(ltrim(main.machine_id)) = '775'  and dtl.adflag = 2
--試了沒?
dawugui 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 yunhaiC 的回复:]
引用 12 楼 dawugui 的回复:
引用 6 楼 yunhaiC 的回复:
where 1= 1 and main.diff_date >= '20081101' and main.diff_date <= '20081202' and main.machine_id = '775' and dtl.adflag = 2

这里的machine_id = '775'就查不出来,其他的id可以查出来,怎么会有这种问题


再次问你,你的字段类型是什么?int , char ,varchar?

跟类型没关系啊,是int类型
[/Quote]
如果是INT类型.machine_id = '775'?
应该是:machine_id = 775
  • 打赏
  • 举报
回复
我发觉在不填machine_id的情况下,查出的资料不含我需要的machine_id,所以加上这个where条件以后查不出来

-->>z这个是正常的啊,只有当一行完全满足你后面where所有的条件的时候才能筛选出来啊。
查不出来,是因为没有满足你条件的行
  • 打赏
  • 举报
回复
我发觉在不填machine_id的情况下,查出的资料不含我需要的machine_id,所以加上这个where条件以后查不出来

但是很奇怪,我觉得主表跟这个machine表有关系吧,虽然中途我也join了一些其他表,但是就是没资料
我如果去掉其他表的join,那样就可以查出我需要的machine_id了
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 dawugui 的回复:]
引用 6 楼 yunhaiC 的回复:
where 1= 1 and main.diff_date >= '20081101' and main.diff_date <= '20081202' and main.machine_id = '775' and dtl.adflag = 2

这里的machine_id = '775'就查不出来,其他的id可以查出来,怎么会有这种问题


再次问你,你的字段类型是什么?int , char ,varchar?
[/Quote]
跟类型没关系啊,是int类型
百年树人 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yunhaiC 的回复:]
where 1= 1 and main.diff_date >= '20081101' and main.diff_date <= '20081202' and main.machine_id = '775' and dtl.adflag = 2

这里的machine_id = '775'就查不出来,其他的id可以查出来,怎么会有这种问题
[/Quote]

可以用machine_id like '%775%' 试试,如果能查出来,那应该就是存在空格或其他不明字符引起的
  • 打赏
  • 举报
回复
一般一个数字型字符的话,sql会自动转换。
出现你上述的原因可能是字符类型不同,一个是字符型,一个是数值型的,但是在字符型的字符串中有了空格

--> (让你望见影子的墙)生成测试数据,时间:2008-12-04

if not object_id('tb') is null
drop table tb
Go
Create table tb([ID] varchar(2),[ParentId] int,[Name] int,[Order] int)
Insert tb
select 1,0,1,1 union all
select 2,0,2,1 union all
select 3,0,3,1 union all
select ' 4',2,4,1 union all
select 5,3,5,1 union all
select 6,4,6,1 union all
select 7,2,7,10 union all
select 8,5,8,1
Go
Select * from tb

select * from tb where id = ' 4' --可以查处
select * from tb where id = '4' --查不出来
解决方法:进行空格处理
select * from tb where ltrim(rtrim(id)) = ltrim(rtrim(' 4')) --就可以查出
shizheyangde 2008-12-04
  • 打赏
  • 举报
回复
数据库中的类型
数据库中的4左右是否有空格
shizheyangde 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 yunhaiC 的帖子:]
比如 where id=3
可以查出相关资料
但是 where id = 4就查不出来
有资料我可以保证
[/Quote]

1.字符类型?
2.=?
3.等号中间的空格?
加载更多回复(14)

22,209

社区成员

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

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