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可以查出来,怎么会有这种问题
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可以查出来,怎么会有这种问题
where 1= 1 and main.diff_date >= '20081101' and main.diff_date <= '20081202' and rtrim(ltrim(main.machine_id)) = '775' and dtl.adflag = 2
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 tbselect * from tb where id = ' 4' --可以查处 select * from tb where id = '4' --查不出来 解决方法:进行空格处理 select * from tb where ltrim(rtrim(id)) = ltrim(rtrim(' 4')) --就可以查出
可以用machine_id like '%775%' 试试,如果能查出来,那应该就是存在空格或其他不明字符引起的
再次问你,你的字段类型是什么?int , char ,varchar?
出现你上述的原因可能是字符类型不同,一个是字符型,一个是数值型的,但是在字符型的字符串中有了空格--> (让你望见影子的墙)生成测试数据,时间: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 tbselect * from tb where id = ' 4' --可以查处
select * from tb where id = '4' --查不出来
解决方法:进行空格处理
select * from tb where ltrim(rtrim(id)) = ltrim(rtrim(' 4')) --就可以查出
可以用machine_id like '%775%' 试试,如果能查出来,那应该就是存在空格或其他不明字符引起的
我如果去掉其他表的join,那样就可以查出我需要的machine_id了
查不出来,是因为没有满足你条件的行
应该是:machine_id = 775
--試了沒?
你可以一个一个的表逐个join,看看是联表的哪个环节出错。
machine_id = '775'
改成
ltrim(rtrim(machine_id)) = '775'
-----------------
用left outer join.
如果这样能出来, 说明你的列中有特殊字符
and main.diff_date >= '20081101' and main.diff_date <= '20081202'
and main.machine_id LIKE '%775%'
and dtl.adflag = 2