[英]sql Complex data sorting with start_time and end_time
I have a MySQL table named 'activity' that contains activity data. 我有一个名为“ activity”的MySQL表,其中包含活动数据。 The important columns are 'start_time' and 'end_time' which contain string (YYYY-MM-DD) to represent when the activity starts and ends.
重要的列是“ start_time”和“ end_time”,其中包含字符串(YYYY-MM-DD)以表示活动开始和结束的时间。
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:
例如:今天是2013-06-23。我需要这样的数据:
--------------------------------
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 |
------------------------------
You can order conditionally on case
like this: 您可以根据以下
case
有条件地订购:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.