数据库结构:
id, subject, content, category, time我想要得到的结果是:首先按category分类(应使用group by category);第二,按time排序,其中最近time的row的category放在最前面,第二近time的row的category放在第二集团。比如,
subject1, category1,time1     -----  时间最新
subject2, category1,time11
subject3, category2,time2     -----  时间第二新
subject4, category2,time22
subject5, category3,time3     -----  时间第三新
subject5, category3,time33
......

解决方案 »

  1.   

    select * from tb order by category,time
      

  2.   

    用聚集函数进行统计才需要group by,你只是排序,不需要分组.
      

  3.   

    select * from tb order by category ,time desc
      

  4.   


    select * from tb order by category asc,time desc
      

  5.   

    根据你的结果,group by 就不需要了,直接查询:
    select * from tb order by category,time
      

  6.   

    select * from tb order by category,time descmodify
      

  7.   

    貌似理解上有些差错:按time排序,其中最近time的row的category放在最前面,第二近time的row的category放在第二集团。 你的时间最新是每个category的最新时间还是所有的category的最新时间?
      

  8.   

    select * from tb order by category,time desc
      

  9.   


    每个category有多条记录,把时间最近的一条纪录的category放在最前面,后面跟着同一个category的纪录,时间无所谓了;然后排时间第二近的一条记录的category,后面跟着此category的纪录,以次类推。
      

  10.   

    如果是这样的话,则要:
    select a.* from tb a inner join (select category,min(time) as mintime from tb group by category) b on a.category=b.category order by b.mintime desc,a.time desc
      

  11.   

    select * from tb a order by (select max(time) from category b where a.id=b.id) desc ,time
      

  12.   


    那么有个问题,如果排时间第二近的一条记录的category和 排时间第一近的category相同的话怎么处理?
      

  13.   


    时间太长了,我在中间加了一个限制,
    SQL codeselect a.* from tb a inner join (select category,min(time) as mintime from tb group by category) b on a.time>'2009-04-09' and a.category=b.category order by b.mintime de…结果出现很多的重复纪录
      

  14.   


    select * from tb a order by (select max(time) from category b where a.id=b.id) desc ,time为什么不看看我的啊!
      

  15.   


    因为有的category有空格之类的,比如"car", "car ",所以本应该算一类,怎么trim一下呢?
      

  16.   


    select * from tb a
    group by *
     order by (select max(time) from category b where a.id=b.id) desc ,time