有一表tb
有几个列
id   ---id
title --标题
postdate --- 发布时间
istop(bit)  --- 是否置顶
topdate(datetime) ---置顶有效期 可以为null 如果为null的话则表示永久有效
现在查询后要排序的结果是
先(istop为true)且(topdate为null或topdate<DateTime.Now)的行
余下不符合上面这个条件的行按postdate排
麻烦写个这样的让我看看好吗,因为存储过程不是特别熟练,试了一两天了都没尝试出来

解决方案 »

  1.   

    先(istop为true)且(topdate为null或topdate <DateTime.Now)的行 
     ----先(istop为true)且(topdate为null或topdate <DateTime.Now)的行,符合这些条件的行也是按postdate desc排序
      

  2.   


    select *
    from table
    order by case when XXXX then XX else XX end
      

  3.   

    select * from tb order by case when istop = 1 and (topdate is null or topdate < getdate()) then 1 else 2 end , postdate
      

  4.   


    如果你的id是自增的,postdate换成id会快些
      

  5.   

    Try:
    ORDER BY (CASE [istop] WHEN TRUE THEN 1 ELSE 2 END),
     (CASE [topdate] WHEN NULL THEN 1 ELSE 2 END),--你的“topdate <DateTime.Now”這個條件是多餘的
     [postdate]
      

  6.   

    这句差点就可以了select * from [News] order by case when istop = 1 and (topdate is null or topdate < getdate()) then 1 else 2 end , postdate desc像有3列值一列是
    id  title postdate istop topdate
    1    'a'   2008-1-1  true  2008-12-31
    2   'b'    2008-2-1  true  2008-3-1 (这个日期已经小于DateTime.Now了)
    3   'c'    2008-3-1  false  null
    4   'd'    2008-4-1  true  null
    那么我想排列顺序是  4-1-3-2
    但是按照dawugui的写出来排序成了4-1-2-3了
      

  7.   

    我觉得topdate <DateTime.Now”這個條件并不多余
    因为我想让一些新闻置顶 可以设置无限期置顶 也可以设置置顶有效期
      

  8.   

    select * from tb order by case when istop = 1 and (topdate is null or topdate < getdate()) then 1 else 2 end , postdate--id自增,换成ID
    select * from tb order by case when istop = 1 and (topdate is null or topdate < getdate()) then 1 else 2 end , [ID]