数据库表:
id topic_id      post_date
1    1          2012-10-02
2    1          2012-11-03
3    2          2012-11-10
4    2          2012-11-04
5    3          2012-11-06
6    3          2012-08-06期望结果:id topic_id      post_date
3    2          2012-11-10
4    2          2012-11-04
5    3          2012-11-06
6    3          2012-08-06
2    1          2012-11-03
1    1          2012-10-02按照topic_id分组, 然后每组内按照post_date排序, 整个分组按照组内第一个日期降序排列。

解决方案 »

  1.   

    select *
    from tb
    order by topic_id,post_date desc
      

  2.   

    select *, (select max(post_date from) 数据库表 where topic_id=a.topic_id ) as m
    from 数据库表 a
    order by m desc,topic_id
      

  3.   

    有高手在吗??
        跪求帮助
    LRMXPeEroeOSfM72Q6gPJ/SSI1YvhHx3ldoLnaIVm8jSUGEBmOOzFmWOEJm8/EcKJFjG9G2dv75/UJGao3vukmVCvT4LK30d 
      由66个中文字 组成的数据。。你能解吗??
        重金酬谢 谢谢 能解联系我 15085642252 QQ 85407665
         本人有很多这样的数据需要解 1000元RMB 做为回报 绝不实言
      

  4.   

    按两个字段进行排序,先按topic_id进行排序,topic_id相同的再按post_date进行排序
      

  5.   

    问一下啊,这样写的话,topic_id为1的两组为什么不在最上面呢?想不明白啊  
      

  6.   

    select * from table1 group by topic_id order by  post_date desc
      

  7.   

    人家说的是以topic_id分组 然后以post_date排序  不是以topic_id这个排序后再以post_date排序
      

  8.   


    select * 
    from tb
     order by topic_id,post_date desc 
      

  9.   

    mysql> select a.id, a.topic_id, a.post_date, (select max(post_date) from t1 wher
    e topic_id=a.topic_id) as m from t1 a order by m desc, topic_id;
    +------+----------+------------+------------+
    | id   | topic_id | post_date  | m          |
    +------+----------+------------+------------+
    |    3 |        2 | 2012-11-10 | 2012-11-10 |
    |    4 |        2 | 2012-11-04 | 2012-11-10 |
    |    5 |        3 | 2012-11-06 | 2012-11-06 |
    |    6 |        3 | 2012-08-06 | 2012-11-06 |
    |    1 |        1 | 2012-10-02 | 2012-11-03 |
    |    2 |        1 | 2012-11-03 | 2012-11-03 |
    +------+----------+------------+------------+
    6 rows in set (0.00 sec)
    只取前3列就行。
      

  10.   

    效果是一样的啊,根据topic_id进行排序不久是把相同的topic_id放在一起了吗,这样也达到分组的目的了呀
      

  11.   

    你仔细看一下他的topic_id这列 是 如果是一楼的方法的话 肯定是 这样的
      

  12.   

    恩,topic_id确实是像你说的那样,我之前看他的要求只是按topic_id进行分组,没有注意topic_id还必须按照他给出的顺序进行排序.
      

  13.   

    我刚试了一下,LZ的那种排序方式还有点小问题,如果是按一楼的语句来,不是LZ要的结果,如果以我的来也不是LZ要的结果。所以我觉得我们还得好好想想......
      

  14.   

    select * from table_name order by topic_id,post_date desc
      

  15.   

    select * from tb order by topic_id,post_date desc   ---topic_id 这个放前面
      

  16.   


    select *
    from tb
    order by topic_id,post_date desc1楼的sql可以满足他的这个需求
    +!