douang1243 2016-12-30 09:37
浏览 242

sql使用start_time和end_time进行复杂数据排序

I have a MySQL table named 'activity' that contains activity data. The important columns are 'start_time' and 'end_time' which contain string (YYYY-MM-DD) to represent when the activity starts and ends.

I want to get some records at one time.Kind of like paging.The problem is I need to sort the data. Sorting rules:

 1:data which current time between start_time and end_time row first
 2:data which current time before start_time second
 3:data which current time after end_time last

Events:

--------------------------------
ID | START_TIME      | END_TIME|
--------------------------------
1  | 2013-06-14 | 2013-06-14 |
2  | 2013-07-01 | 2013-07-10 |
3  | 2013-07-30 | 2013-07-31 |
4  | 2013-06-15 | 2013-08-21 |
5  | 2013-06-22 | 2013-06-25 |
------------------------------

Don't you know how to do that? Any suggestions?

Thanks! Example: Today is 2013-06-23.I need data like this:

 --------------------------------
ID | START_TIME      | END_TIME|
--------------------------------
4  | 2013-06-15 | 2013-08-21 |
5  | 2013-06-22 | 2013-06-25 |
2  | 2013-07-01 | 2013-07-10 |
3  | 2013-07-30 | 2013-07-31 |
1  | 2013-06-14 | 2013-06-14 |
------------------------------
  • 写回答

1条回答 默认 最新

  • duanken7168 2016-12-30 09:41
    关注

    You can order conditionally on case like this:

    select *
    from table
    order by 
    case 
      when '2013-06-23' >= start_time and '2013-06-23' <= end_time then 0
      when '2013-06-23' < start_time then 1
      else 2 end,
    start_time, end_time, id;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用