如何在 PostgreSQL 递归中有两个排序选项
How to have two sort options in PostgreSQL RECURSIVE
我有以下递归组织评论及其回复的查询。
WITH RECURSIVE comment_tree AS (
SELECT
id AS comment_id,
body AS comment_body,
reply_to AS comment_reply_to,
1 AS level,
"createdAt" AS comment_date,
commenter_id,
article_id,
array["createdAt"] AS path_info
FROM "Comments"
WHERE "reply_to" IS NULL
UNION ALL
SELECT
c.id,
c.body,
c.reply_to,
p.level + 1,
"createdAt",
c.commenter_id,
c.article_id,
p.path_info || c."createdAt"
FROM "Comments" c
JOIN comment_tree p ON c.reply_to = comment_id
)
SELECT
comment_id,
path_info,
comment_body,
comment_reply_to,
comment_date,
level,
U.first_name,
U.last_name,
coalesce(U.username, CAST(U.id AS VARCHAR)) AS username
FROM comment_tree
LEFT JOIN
"Users" U ON commenter_id = U.id
WHERE article_id = '62834723-B804-4CA1-B984-D949B1A7E1E2'
ORDER BY path_info DESC;
据我所见...到目前为止,除了排序外,一切正常。
目前评论按从旧到新的顺序排列。然后正确地将回复嵌套在下面,但我希望父列表从最新到最旧。
有什么方法可以对子值 DESC 和父值 ASC 进行排序?
例如
+----+----------+----------+
| id | reply_to | date |
+----+----------+----------+
| C1 | null | 01052016 | < - Oldest
| C2 | null | 02052016 |
| C3 | C1 | 03052016 |
| C4 | C1 | 04052016 |
| C5 | null | 05052016 |
| C6 | C4 | 06052016 |
| C7 | C2 | 07052016 |
| C8 | C6 | 08052016 | < - Newest
| | | |
+----+----------+----------+
想要的结果
| C5 (Newest Parent first)
| C2
| C7
| C1
| C3 (Oldest Child first for all tiers below parent)
| C4
| C6
| C8
我会在 Common Table Expression.
中引入一个人工列 sort
Comments
定义如下:
Table "laurenz.Comments"
┌───────────┬───────────────────────┬───────────┐
│ Column │ Type │ Modifiers │
├───────────┼───────────────────────┼───────────┤
│ id │ character varying(10) │ not null │
│ reply_to │ character varying(10) │ │
│ createdAt │ date │ not null │
└───────────┴───────────────────────┴───────────┘
Indexes:
"comment_tree_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"comment_tree_reply_to_fkey" FOREIGN KEY (reply_to) REFERENCES "Comments"(id)
Referenced by:
TABLE ""Comments"" CONSTRAINT "comment_tree_reply_to_fkey" FOREIGN KEY (reply_to) REFERENCES "Comments"(id)
我会这样写:
WITH RECURSIVE comment_tree AS (
SELECT id, reply_to, "createdAt",
CAST(current_date - "createdAt" AS text) AS sort
FROM "Comments"
WHERE reply_to IS NULL
UNION ALL SELECT c.id, c.reply_to, c."createdAt",
substring(p.sort FROM '^[^-]*') || '-' || c."createdAt"
FROM "Comments" c
JOIN comment_tree p ON c.reply_to = p.id
)
SELECT id, reply_to, "createdAt"
FROM comment_tree
ORDER BY sort;
我有以下递归组织评论及其回复的查询。
WITH RECURSIVE comment_tree AS (
SELECT
id AS comment_id,
body AS comment_body,
reply_to AS comment_reply_to,
1 AS level,
"createdAt" AS comment_date,
commenter_id,
article_id,
array["createdAt"] AS path_info
FROM "Comments"
WHERE "reply_to" IS NULL
UNION ALL
SELECT
c.id,
c.body,
c.reply_to,
p.level + 1,
"createdAt",
c.commenter_id,
c.article_id,
p.path_info || c."createdAt"
FROM "Comments" c
JOIN comment_tree p ON c.reply_to = comment_id
)
SELECT
comment_id,
path_info,
comment_body,
comment_reply_to,
comment_date,
level,
U.first_name,
U.last_name,
coalesce(U.username, CAST(U.id AS VARCHAR)) AS username
FROM comment_tree
LEFT JOIN
"Users" U ON commenter_id = U.id
WHERE article_id = '62834723-B804-4CA1-B984-D949B1A7E1E2'
ORDER BY path_info DESC;
据我所见...到目前为止,除了排序外,一切正常。
目前评论按从旧到新的顺序排列。然后正确地将回复嵌套在下面,但我希望父列表从最新到最旧。
有什么方法可以对子值 DESC 和父值 ASC 进行排序?
例如
+----+----------+----------+
| id | reply_to | date |
+----+----------+----------+
| C1 | null | 01052016 | < - Oldest
| C2 | null | 02052016 |
| C3 | C1 | 03052016 |
| C4 | C1 | 04052016 |
| C5 | null | 05052016 |
| C6 | C4 | 06052016 |
| C7 | C2 | 07052016 |
| C8 | C6 | 08052016 | < - Newest
| | | |
+----+----------+----------+
想要的结果
| C5 (Newest Parent first)
| C2
| C7
| C1
| C3 (Oldest Child first for all tiers below parent)
| C4
| C6
| C8
我会在 Common Table Expression.
中引入一个人工列sort
Comments
定义如下:
Table "laurenz.Comments"
┌───────────┬───────────────────────┬───────────┐
│ Column │ Type │ Modifiers │
├───────────┼───────────────────────┼───────────┤
│ id │ character varying(10) │ not null │
│ reply_to │ character varying(10) │ │
│ createdAt │ date │ not null │
└───────────┴───────────────────────┴───────────┘
Indexes:
"comment_tree_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"comment_tree_reply_to_fkey" FOREIGN KEY (reply_to) REFERENCES "Comments"(id)
Referenced by:
TABLE ""Comments"" CONSTRAINT "comment_tree_reply_to_fkey" FOREIGN KEY (reply_to) REFERENCES "Comments"(id)
我会这样写:
WITH RECURSIVE comment_tree AS (
SELECT id, reply_to, "createdAt",
CAST(current_date - "createdAt" AS text) AS sort
FROM "Comments"
WHERE reply_to IS NULL
UNION ALL SELECT c.id, c.reply_to, c."createdAt",
substring(p.sort FROM '^[^-]*') || '-' || c."createdAt"
FROM "Comments" c
JOIN comment_tree p ON c.reply_to = p.id
)
SELECT id, reply_to, "createdAt"
FROM comment_tree
ORDER BY sort;