我的桌子看起来像是:
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
。
感谢任何帮助。
由于
答案 0 :(得分:2)
你在说这个吗?
ORDER BY root_id DESC, is_root DESC, timestamp
答案 1 :(得分:2)
order by root_id desc, is_root desc, timestamp asc
应该做的伎俩。