id   name   totop     inputdate
1    zxz     1        2006-3-16
2    cxx     0        2006-3-16
3    sss     1        2003-3-15大概是这样的,就是我想排序,如果totop是1 而且是日期是最近一天的数据,那么显示在最前面,否则,不管TOTOP是什么数值,都按ID倒叙排列
怎么写?
谢谢

解决方案 »

  1.   

    select 
        a.* 
    from 
        表 a 
    order by 
        case 
          when a.totop=1 and not exists(select 1 from 表 where inputdate>a.inputdate) then 0
          else 1
        end,a.id desc
      

  2.   

    select * from tbl order by case when totop=1 then -1 else 0 end asc,inputdate desc