各位大侠帮帮忙,数据库语句的优化
是查出每个基站号所对应的年和月最大的记录,谢谢各位!非常着急
select * from jl_stationweekreport as a where a.month=(select max(month) from jl_stationweekreport as b where a.stationcode=b.stationcode and a.year=b.year 
and b.year=(select max(year) from jl_stationweekreport as c where b.stationcode=c.stationcode))

解决方案 »

  1.   

    贴出你的show index from jl_stationweekreport;和explain select * from jl_stationweekreport as a where a.month=(select max(month) from jl_stationweekreport as b where a.stationcode=b.stationcode and a.year=b.year 
    and b.year=(select max(year) from jl_stationweekreport as c where b.stationcode=c.stationcode))
      

  2.   

    stationcode 基站号
    year    年
    month   月
    该sql是从jl_stationweekreport 表中查出每个基站号所对应的年和月最大的记录
    谢谢各位
      

  3.   

    看来楼主并不愿意提供更多的信息了。随便猜一下,你用下面的语句吧。select * 
    from jl_stationweekreport as a
    where not exists (
    select 1 from jl_stationweekreport 
    where stationcode=a.jl_stationweekreport 
    and (year>a.year or ((year=a.year) and month>a.month))
    );
    当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
      

  4.   

    谢谢CHM,不是不愿意提供信息,只是不知该提供哪些信息,你的sql就是我要的,非常感谢