表1新闻 table1 字段有id,name
表2评论 table2 字段有id,fid,content,time表2的fid对应表1的id排列新闻,按照每条新闻的最后一个评论的时间顺序,倒叙排列新闻注:每个新闻对应很多评论,现在需要按照最后一个评论的时间来排,并且需要排列出来的新闻列表分页

解决方案 »

  1.   

    table2中fid应该对应table1的id吧?

    select t1.id,t1.name from table1 t1,(select fid,time from table2 order by time desc limit 1) t2 where t1.id=t2.fid order by t2.time desc,t1.id desc
      

  2.   

    select t.id,t.name,tt.id,tt.fid,tt.content,tt.time from table1 t,table2 tt where t.id = tt.fid order by UNIX_TIMESTAMP(tt.time) desc
      

  3.   

    对了,要分页。。
    select t.id,t.name,tt.id,tt.fid,tt.content,tt.time from table1 t,table2 tt where t.id = tt.fid order by UNIX_TIMESTAMP(tt.time) desc limit 0 ,10
      

  4.   

    试试这个select b.id,b.name,a.time from((select fid,max(id),time from table2 group by fid )a left join (select id,name from table1)b on a.fid=b.id) order by b.time asc 
      

  5.   

    忘记时间撮了。
    select b.id,b.name,a.t from((select fid,max(id),UNIX_TIMESTAMP(time) as t from table2 group by fid )a left join (select id,name from table1)b on a.fid=b.id) order by a.t desc
      

  6.   

    补充一点,就是有的新闻可能是没有评论的,那么此条新闻就排到后面
    数据表中的time 是时间戳格式的
      

  7.   

    --ORACLE 8I 测试通过,分页可通过限制IID的范围来获得
    select * from 
    (select rownum iid,t1.art_no,t2,buyer_uid from table1 t1,table2 t2 
        where t1.id=t2.fid order by t2.time desc)
     where iid between 2 and 5
      

  8.   

    order by t2.time desc 就已经把没有评论的新闻放到最后了,也不晓得你的数据库是啥
      

  9.   

    你用的是什么数据库?不同数据库支持的SQL语句不同!
      

  10.   

    select t1.id,t1.name from table1 as t1,table2 as t2 where t1.id=t2.fid order by t2.time desc limit 10;
    MySQL数据库