1

我有一个看起来像的表:

 id | user_id | activity_id | activity_type | root_id | is_root | timestamp 
----+---------+-------------+---------------+---------+---------+-----------
  1 |       1 |           1 | text          |       1 |       1 |         5
  2 |       2 |           2 | text          |       1 |       0 |         6
  3 |       3 |           3 | text          |       1 |       0 |        10
  4 |       2 |          10 | text          |      10 |       1 |        50
  5 |       1 |          11 | text          |      10 |       0 |        90
  6 |       3 |          12 | text          |      10 |       0 |       100
  7 |       3 |          20 | text          |      20 |       1 |       190
  8 |       2 |          21 | text          |      20 |       0 |       130
  9 |       3 |          22 | text          |      20 |       0 |       150
 10 |       3 |          22 | text          |      20 |       0 |       150
 11 |       3 |          22 | text          |      20 |       0 |       150

我想要一个像

 id | user_id | activity_id | activity_type | root_id | is_root | timestamp 
----+---------+-------------+---------------+---------+---------+-----------
  7 |       3 |          20 | text          |      20 |       1 |       120
  8 |       2 |          21 | text          |      20 |       0 |       130
 11 |       3 |          22 | text          |      20 |       0 |       150
  9 |       3 |          22 | text          |      20 |       0 |       150
 10 |       3 |          22 | text          |      20 |       0 |       150
  4 |       2 |          10 | text          |      10 |       1 |        50
  5 |       1 |          11 | text          |      10 |       0 |        90
  6 |       3 |          12 | text          |      10 |       0 |       100
  1 |       1 |           1 | text          |       1 |       1 |         5
  2 |       2 |           2 | text          |       1 |       0 |         6
  3 |       3 |           3 | text          |       1 |       0 |        10

root_id 应该放在一个组中,并且该组的第一行应该有 is_root = 1。组应该根据根 DESC 的时间戳排序,但根的孩子应该按 ASC 排序(基于时间戳)

The relevant columns for the question is root_id, is_root, timestamp.

任何帮助表示赞赏。

谢谢

4

2 回答 2

2

你在说这个吗?

ORDER BY root_id DESC, is_root DESC, timestamp
于 2011-07-08T19:59:41.853 回答
2
order by root_id desc, is_root desc, timestamp asc

应该做的伎俩。

于 2011-07-08T20:00:36.313 回答