MySQL按字段值和id排序

时间:2017-06-10 14:44:41

标签: php mysql

id | category | date
--------------------
 1 | three | 2017-06-02
 2 | one | 2017-06-02
 3 | two | 2017-06-02
 4 | three | 2017-06-02
 5 | one | 2017-06-05
 6 | two | 2017-06-05
 7 | three | 2017-06-05
 8 | one | 2017-06-08
 9 | one | 2017-06-08
 10 | two | 2017-06-08
 11 | three | 2017-06-08
 12 | two | 2017-06-10

我希望使用类别字段顺序(一,二,三)获取结果,同时仍然按ID DESC维护顺序。我不打算使用日期字段,仅用于视觉。结果应如下所示。

id | category | date
--------------------
 12 | two | 2017-06-10
 8 | one | 2017-06-08
 9 | one | 2017-06-08
 10 | two | 2017-06-08
 11 | three | 2017-06-08
 5 | one | 2017-06-05
 6 | two | 2017-06-05
 7 | three | 2017-06-05
 2 | one | 2017-06-02
 3 | two | 2017-06-02
 1 | three | 2017-06-02
 4 | three | 2017-06-02

1 个答案:

答案 0 :(得分:0)

您的订购似乎是按日期desc,然后是类别,如下所示:

order by date desc, field(category, 'one', 'two', 'three')