如果是字符型
where id = '4'
where cast(id as varchar) = '4'如果是数值型
where id = 4
where cast(id as int) = 4还查不出来,就不知道了.或者再试试
order by id 
然后看你需要的4在什么位置.

解决方案 »

  1.   

    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可以查出来,怎么会有这种问题
      

  2.   

    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可以查出来,怎么会有这种问题
      

  3.   

    where 1= 1 and main.diff_date >= '20081101'  and main.diff_date <= '20081202' and rtrim(ltrim(main.machine_id)) = '775'  and dtl.adflag = 2
      

  4.   


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

  5.   

    一般一个数字型字符的话,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 tbselect * from tb where id = ' 4'  --可以查处
    select * from tb where id = '4'   --查不出来
    解决方法:进行空格处理
    select * from tb where ltrim(rtrim(id)) = ltrim(rtrim(' 4')) --就可以查出
      

  6.   


    可以用machine_id like '%775%' 试试,如果能查出来,那应该就是存在空格或其他不明字符引起的
      

  7.   

    我发觉在不填machine_id的情况下,查出的资料不含我需要的machine_id,所以加上这个where条件以后查不出来但是很奇怪,我觉得主表跟这个machine表有关系吧,虽然中途我也join了一些其他表,但是就是没资料
    我如果去掉其他表的join,那样就可以查出我需要的machine_id了
      

  8.   

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

  9.   

    如果是INT类型.machine_id = '775'?
    应该是:machine_id = 775
      

  10.   

    where 1= 1 and main.diff_date >= '20081101'  and main.diff_date <= '20081202' and rtrim(ltrim(main.machine_id)) = '775'  and dtl.adflag = 2
    --試了沒?
      

  11.   

    hehe,早猜到是这样……因为不太正确的联表导致记录为空。
    你可以一个一个的表逐个join,看看是联表的哪个环节出错。
      

  12.   


    machine_id = '775'
    改成
    ltrim(rtrim(machine_id)) = '775'
      

  13.   

    虽然中途我也join了一些其他表
    -----------------
    用left outer join.
      

  14.   

    你用 like '%775%'
    如果这样能出来, 说明你的列中有特殊字符
      

  15.   

    你写成where id = 3试试
      

  16.   

    where 1= 1 
        and main.diff_date >= '20081101'  and main.diff_date <= '20081202' 
        and main.machine_id LIKE '%775%'  
        and dtl.adflag = 2