SQL:如何检索按日期和 ID 排序的记录,以日期顺序为主?
SQL: How to retrieve records ordering by date and ID for with date order predominating?
我有一个 table,内容如下:
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
我需要从 table 按日期排序和 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
更新 #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
我有一个 table,内容如下:
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
我需要从 table 按日期排序和 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
更新 #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