SQL:如何检索按日期和ID排序的记录,日期顺序占主导地位?

时间:2015-01-03 05:26:24

标签: mysql sql

我有一张包含以下内容的表格:

id  Event_id    Event_name  Entry_time

1   2           Cricket     2015-01-03 09:58:08
2   2           Cricket     2015-01-03 09:58:08
3   1           Drama Club  2015-01-03 09:58:37
4   1           Drama Club  2015-01-03 09:58:37
5   NULL        NULL        2015-01-03 09:58:58
6   2           Cricket     2015-01-03 10:00:32
7   2           Cricket     2015-01-03 10:00:32
8   1           Drama Club  2015-01-03 10:01:40
9   1           Drama Club  2015-01-03 10:01:40
10  2           Cricket     2015-01-04 10:03:40
11  1           Drama Club  2015-01-04 10:01:40

我需要从日期和event_id排序的表中检索记录,日期顺序占主导地位,例如。今天的记录第一,昨天的记录第二,等等。

当我执行下面的查询时,记录按照我的要求以错误的顺序出现。我需要首先显示Drama Club记录,因为我在Drama club中创建了最后一个条目。如果有人知道我哪里出错了,请帮助我。

SELECT * 
FROM  `event_info` 
ORDER BY DATE( Entry_time ) DESC , Event_id DESC , Event_id DESC

(错误排序)结果:

id  Event_id    Event_name  Entry_time

1   2           Cricket     2015-01-03 09:58:08
2   2           Cricket     2015-01-03 09:58:08
6   2           Cricket     2015-01-03 10:00:32
7   2           Cricket     2015-01-03 10:00:32
3   1           Drama Club  2015-01-03 09:58:37
4   1           Drama Club  2015-01-03 09:58:37
8   1           Drama Club  2015-01-03 10:01:40
9   1           Drama Club  2015-01-03 10:01:40
5   NULL        NULL    2015-01-03 09:58:58

我需要低于输出。

id  Event_id    Event_name  Entry_time   
10  2           Cricket     2015-01-04 10:03:40
11  1           Drama Club  2015-01-04 10:01:40 //4th  jan section

3   1           Drama Club  2015-01-03 09:58:37    
4   1           Drama Club  2015-01-03 09:58:37    
8   1           Drama Club  2015-01-03 10:01:40    
9   1           Drama Club  2015-01-03 10:01:40    
1   2           Cricket     2015-01-03 09:58:08    
2   2           Cricket     2015-01-03 09:58:08
6   2           Cricket     2015-01-03 10:00:32    
7   2           Cricket     2015-01-03 10:00:32    
5   NULL        NULL        2015-01-03 09:58:58 //3rd jan section

1 个答案:

答案 0 :(得分:1)

更新#2015-01-05 / 1

我认为现在我理解了这个概念 - 请尝试以下内容:

SELECT 
    A.* 
FROM  
    `event_info` AS A 
    LEFT OUTER JOIN
    (
        SELECT 
            Event_id,
            DATE(Entry_time) AS _Event_Date,
            MAX(TIME(Entry_time)) AS Event_Max_Entry_Time
        FROM  
            `event_info`
        GROUP BY 
            Event_id, DATE(Entry_time)
    ) AS B
      ON A.Event_id = B.Event_id
          AND DATE(A.Entry_time) = B._Event_Date  
ORDER BY 
    DATE(A.Entry_time) DESC, 
    B.Event_Max_Entry_Time DESC, 
    A.Event_id DESC, 
    A.Entry_time ASC, 
    A.Event_id DESC

上面的查询给了我确切的新预期集:

ID  EVENT_ID    EVENT_NAME  ENTRY_TIME

10  2           Cricket     2015-01-04 10:03:40
11  1           Drama Club  2015-01-04 10:01:40 
3   1           Drama Club  2015-01-03 09:58:37
4   1           Drama Club  2015-01-03 09:58:37
8   1           Drama Club  2015-01-03 10:01:40
9   1           Drama Club  2015-01-03 10:01:40
2   2           Cricket     2015-01-03 09:58:08
1   2           Cricket     2015-01-03 09:58:08
7   2           Cricket     2015-01-03 10:00:32
6   2           Cricket     2015-01-03 10:00:32
5   NULL        NULL        2015-01-03 09:58:58