douchuoxuan3177 2012-10-30 09:56
浏览 63
已采纳

查询首先对即将到来的日期,升序,过去日期,降序进行排序

I have a event table which stored date (m/d/Y) in a following manner

eventid     eventstart      eventend    status
----------------------------------------------
      1     10/9/2012       10/27/2012  Active
      2     4/3/2012        4/27/2012   Active
      3     10/26/2012      10/27/2012  Active
      4     2/7/2012        2/9/2012    Active
      5     10/30/2012      10/31/2012  Active
      6     10/9/2012       10/31/2012  Active
      7     11/9/2012       10/19/2012  Active
      8     10/31/2012      10/18/2012  Active

If I have an input date e.g. 10/29/2012 then I want to sort the startdate in the following manner

eventstart          
----------
10/30/2012
10/31/2012 
11/9/2012
10/26/2012
10/9/2012
10/9/2012

Can anyone help me?

  • 写回答

3条回答 默认 最新

  • donglu9134 2012-10-30 10:11
    关注

    You can use the CASE syntax in the order by clause to sort the two cases separately:

    SELECT *
    FROM `events`
    ORDER BY 
        CASE WHEN eventstart >= '2012-10-29' THEN eventstart ELSE '9999-12-31' END ASC, 
        CASE WHEN eventstart <  '2012-10-29' THEN eventstart ELSE NULL         END DESC
    
    -- eventid | eventstart
    -- --------+-----------
    -- 5       | 2012-10-30
    -- 8       | 2012-10-31
    -- 7       | 2012-11-09
    -- 3       | 2012-10-26
    -- 1       | 2012-10-09
    -- 6       | 2012-10-09
    -- 2       | 2012-04-03
    -- 4       | 2012-02-07
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真
  • ¥15 关于#c语言#的问题,请各位专家解答!