基于日期和另一个数字列的MySQL排序表

时间:2016-05-08 18:17:55

标签: mysql sql sorting

假设我有一个这样的表:

+----+--------------+-----+-----------+
| ID |Date          | DD  |    City   | 
+----+--------------+-----+-----------+
|  2 | 2016-05-14   | 102 | NY        |
|  5 | 2016-05-12   | 101 | London    | 
|  3 | 2016-05-13   | 101 | Tokyo     |
|  6 | 2016-05-12   | 102 | LA        |
|  4 | 2016-05-14   | 101 | KC        |
|  7 | 2016-05-13   | 102 | Delhi     |
+----+--------------+-----+-----------+

如何使用基于Date和DD列的MySQL和SQL语句获得Sorted结果,如下所示:

+----+--------------+-----+-----------+
| ID |Date          | DD  |    City   | 
+----+--------------+-----+-----------+
|  2 | 2016-05-12   | 101 | NY        |
|  5 | 2016-05-12   | 102 | London    | 
|  3 | 2016-05-13   | 101 | Tokyo     |
|  6 | 2016-05-13   | 102 | LA        |
|  4 | 2016-05-14   | 101 | KC        |
|  7 | 2016-05-14   | 102 | Delhi     |
+----+----------+-----+-----------+---+

它表示按日期列排序表,但每天也按DD排序。

当我使用ORDER BY Date时,DD输出就像这样

    +----+--------------+-----+-----------+
| ID |Date          | DD  |    City   | 
+----+--------------+-----+-----------+
|  2 | 2016-05-12   | 101 | NY        |
|  5 | 2016-05-13   | 101 | London    | 
|  3 | 2016-05-14   | 101 | Tokyo     |
|  6 | 2016-05-12   | 102 | LA        |
|  4 | 2016-05-13   | 102 | KC        |
|  7 | 2016-05-14   | 102 | Delhi     |
+----+----------+-----+-----------+---+

对我来说不是这样。

2 个答案:

答案 0 :(得分:0)

SELECT * FROM `table` ORDER BY `Date`,`DD`

答案 1 :(得分:-1)

SELECT * FROM table ORDER BY Date ASC,DD DESC