select COUNT(*)  from  Trajector_Tmpschedule tss,  Trajector_Tmpschedule  tse,aftn_tmpflightschedule   
where 
aftn_tmpflightschedule.id=tse.flight_schedule_id 
and   aftn_tmpflightschedule.id=tss.flight_schedule_id  
and tss.routeseq<tse.routeseq  
and tss.legseq=tse.legseq   and tse.istoday='0' 
and tss.pointid='2051' and tse.pointid='2100'
and aftn_tmpflightschedule.days like  '%4%'
and (
(aftn_tmpflightschedule.validperiodbegin<'2008-11-27' and  aftn_tmpflightschedule.validperiodend>='2008-11-27')
  or(aftn_tmpflightschedule.validperiodbegin is null)
  or( aftn_tmpflightschedule.validperiodbegin is null))
  order by tss.pasttime asc 

解决方案 »

  1.   

    先优化你的sql语句,为什么Trajector_Tmpschedule自己连接自己两次?... from Trajector_Tmpschedule tss,  Trajector_Tmpschedule  tse...还有条件
    aftn_tmpflightschedule.id=tse.flight_schedule_id 
    and  aftn_tmpflightschedule.id=tss.flight_schedule_id  
      

  2.   

    你想写的语句是否可以这样优化?
    select COUNT(*)  
    from  Trajector_Tmpschedule a, aftn_tmpflightschedule  b 
    where 
    b.id=a.flight_schedule_id and  a.istoday='0' 
    and (a.pointid='2051' or a.pointid='2100' )
    and b.days like  '%4%' 
    and ((b.validperiodbegin <'2008-11-27' and  b.validperiodend>='2008-11-27') or (b.validperiodbegin is null)) 
    order by tss.pasttime asc