【发布时间】:2017-03-17 17:03:05
【问题描述】:
我有一个 sql 查询,结果如下表。
+-------+----------+---------------------+--------------------------------------+
| tp_id | tp_title | tp_completed_at | uto_order |
+-------+----------+---------------------+--------------------------------------+
| 69033 | task3 | 2017-03-17 08:15:38 | 3401893506280706400000000.0000000000 |
| 69022 | post 1 | 2017-03-15 10:24:03 | 1122274146401882300000000.0000000000 |
| 69032 | task4 | 2017-03-17 08:15:35 | 810531327956915000000000.0000000000 |
| 68998 | final | 2017-03-13 14:23:18 | 498788509511947700000000.0000000000 |
| 68978 | app4 | 2017-03-13 14:05:39 | 221683782005310100000000.0000000000 |
| 68850 | 7 | 2017-02-24 07:58:38 | 12974633789062503000000.0000000000 |
| 68845 | 6 | 2017-02-24 07:54:14 | 5766503906250001000000.0000000000 |
+-------+----------+---------------------+--------------------------------------+
我想根据tp_completed_at 和uto_order 分别对结果进行排序。我的意思是,首先查询应该根据tp_completed_at 对结果进行排序,然后它应该再次根据uto_order 对在tp_completed_at 中具有相同日期的记录(不包括时间戳)对整个数据进行排序,例如,任务3 和根据tp_completed_at排序后,任务4将彼此相邻,如下所示
+-------+----------+---------------------+--------------------------------------+
| tp_id | tp_title | tp_completed_at | uto_order |
+-------+----------+---------------------+--------------------------------------+
| 68845 | 6 | 2017-02-24 07:54:14 | 5766503906250001000000.0000000000 |
| 68850 | 7 | 2017-02-24 07:58:38 | 12974633789062503000000.0000000000 |
| 68978 | app4 | 2017-03-13 14:05:39 | 221683782005310100000000.0000000000 |
| 68998 | final | 2017-03-13 14:23:18 | 498788509511947700000000.0000000000 |
| 69022 | post 1 | 2017-03-15 10:24:03 | 1122274146401882300000000.0000000000 |
| 69032 | task4 | 2017-03-17 08:15:35 | 810531327956915000000000.0000000000 |
| 69033 | task3 | 2017-03-17 08:15:38 | 3401893506280706400000000.0000000000 |
+-------+----------+---------------------+--------------------------------------+
我想再次根据 uto_order 排序(6 和 7),(app4 和 final)基于 uto_order,(task4 和任务 3)基于 uto_order,因为所有这组任务都有 tp_completed_at作为同一天。怎么做?提前谢谢!!!
【问题讨论】:
-
如果两条记录的
tp_completed_at值相同,您的意思是先按tp_completed_at排序sort,然后按uto_order排序? -
就像您解释的那样,如果您对集合进行两次独立排序,则只会根据最后一次排序进行排序。但是,如果您真正想要的是根据
tp_completed_at进行排序,然后对于每个相似的值,根据uto_order对这些块进行排序,只需ORDER BY tp_completed_at, uto_order。请用一个很好的例子来编辑带有预期输出的问题。 -
也许这个答案会帮助你stackoverflow.com/a/42719808/2286537
-
首先,您想按日期时间排序,然后,对于每个相同的日期,在第二列排序。或者可以按日期订购
tp_completed_at吗? -
@Darshan Mehta....是的,但是,我应该只考虑 tp_completed_at 的 yyyy-mm-dd 部分,即当 tp_completed_at 的“yyyy-mm-dd”部分相同时,那么它应该根据 uto_order 排序