简体   繁体   English

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

[英]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.

相关问题 7个SQL行中的第一个开始时间和最后一个结束时间,仅显示1个SQL结果 - First start_time and last end_time from 7 SQL rows to show only 1 SQL result Laravel 5.1获取start_time和end_time之间的行(两个不同的字段) - Laravel 5.1 Fetch Rows between start_time and end_time (two different fields) 获取每个日期 PHP 的 start_time 和 end_time 的总小时数? - Get total hours from start_time and end_time for each date PHP? 我想要`start_time` 和`end_time` 计数如何得到这个? - I want to `start_time` and `end_time` count how can get this? 如何在yii2中使用start_time和end_time获取MySql中已有的记录? - How to fetch already existing records in MySql with start_time and end_time in yii2? PayPal + PHP - 获取收款(开始时间/结束时间) - PayPal + PHP - Fetch incoming payments (start_time / end_time) 使用 PHP 从具有 start_time 和 end_time 的多维数组中查找总小时数? - Find total hours from multi-dimensional array with start_time and end_time using PHP? 如果在范围内找到,如何找到 start_time 和 end_time 范围,然后检查 start_date 和 end_date - How to find the start_time and end_time range if found in the range then check start_date and end_date 我想显示当前时间不在(column)start_time和end_time(column)之间的所有人员记录 - I want to display all personnel records that the current time is not between to the (column)start_time and end_time(column) 更改Facebook“ end_time”的格式 - Change Format of Facebook “end_time”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM