论坛首页有个“最新帖子”,就是按照发帖时间排序,取10条数据:
$sql = "SELECT t.*, f.name FROM threads t, forums f where f.fid=t.fid   ORDER BY t.dateline DESC LIMIT 0, 10";
但是论坛里面有个公告帖子,希望一直显示在这10条最新贴的第一条,比如帖子的ID是10000,该怎么做呢?

解决方案 »

  1.   

    公告
    union all
    最新帖子
      

  2.   

    SELECT t.*, f.name FROM threads t, forums f where f.fid=t.fid ORDER BY (id=10000) DESC, t.dateline DESC LIMIT 0, 10
      

  3.   

    select .. where id = 10000
    union all 
    select .. where id != 10000 order by ..
    limit 9
      

  4.   

    $sql = "SELECT t.*, f.name FROM threads t, forums f where f.fid=t.fid   ORDER BY id=10000 desc , t.dateline DESC LIMIT 0, 10";但效率比较差。下面这个效率上会好一些select * from (    
    (SELECT t.*, f.name FROM threads t, forums f where f.fid=t.fid id=10000)
    union all
    (SELECT t.*, f.name FROM threads t, forums f ORDER BY t.dateline DESC LIMIT 0, 10)
    ) t
    order by id=10000 desc,datelineDESC LIMIT 0, 10
      

  5.   

    我想问一下, 既然知道了贴子的ID是1000, 为什么不直接用1000去取得了呢? 事实上最新的ID是在不停的变化的, 我觉得你应当不能获得最新的ID号吧?
    第二个问题,既然已经按时间顺序把最新的10条取出来了, 为什么还要去取什么最新10条的第一条? 这不都已经有了吗? 
    也许是我理解的不对
      

  6.   

    select * 
    from (    
    (SELECT t.*, f.name,flag=0 FROM threads t, forums f where f.fid=t.fid and id=10000)
    union all
    (SELECT t.*, f.name ,flag=1 FROM threads t, forums f where f.fid=t.fid and id<>10000 ORDER BY t.dateline DESC LIMIT 9)
    ) t
    order by flag,dateline desc 
    试试