select * from ydbdlist where convert(char(6), actdate, 112)='2005-05' and station<'Y'   and  sc#  not   in 

select sc#  from ydbdptby where convert(char(6), proddate, 112)  ='2005-05'
) order by recdate---把<>號,改>號看看

解决方案 »

  1.   

    select a.*
    from ydbdlist a
    inner join ydbdptby b
    on convert(char(6),a.actdate, 112)=convert(char(6), proddate, 112)
    and a.sc#<>b.sc#
    where a.station<>'Y'   
    and convert(char(6),a.actdate, 112)='200505'
    order by a.recdate
      

  2.   

    最好把表結構和其一部分相關的記錄給貼出來和你想要的結果。這樣大家就更好的測試你想要的結果樓主你樣比較難理解。可以將exists來替換not int
      

  3.   

    或試下這個語句
    select * from (select * from ydbdlist where convert(char(6), actdate, 112)='2005-05' and station<'Y'   )Twhere not exists (select 1 from T left join ydbdptby  on convert(char(6),ydbdptby. proddate, 120)='2005-05' and T.sc#=ydbptby.sc#)
      

  4.   

    select T.* from (select * from ydbdlist where convert(char(6), actdate, 112)='2005-05' and station<'Y'   )Twhere not exists (select 1 from T left join ydbdptby  on convert(char(6),ydbdptby. proddate, 120)='2005-05' and T.sc#=ydbptby.sc#)
      

  5.   

    select * from ydbdlist 
    where --convert(char(6), actdate, 112)='200505' 
    actdate>='2005-5-1' and actdate<'2006-1-1'  --直接在字段上查询,不要把字段包含在函数中
    and station<>'Y'
    and  not exists(  --exists 比 not in 效率好
    select * from ydbdptby 
    where ydbdptby.s#=ydbdlist.s#
    -- and  convert(char(6), proddate, 112)  ='200505'
    and proddate>='2005-5-1' and proddate<'2006-1-1'  --直接在字段上查询,不要把字段包含在函数中
    ) order by recdate
      

  6.   

    另外,需要在 s#,actdate,proddate 字段上分别建立索引. station字段也可以考虑建立索引
    在将字段包含在函数中,会导致索引失效,而索引是提高查询效率的关键,所以任何把字段放在函数中,或者对字段进行计算的处理都应该想办法改掉.
      

  7.   

    谢谢大家 用  zjcxc(邹建) 的方法 查询用了22秒 而原来的方法要用27秒 提高5秒