具有递归 CTE 的 Postgres:按受欢迎程度对子项进行排序/排序,同时保留树结构(父项始终高于子项)

LaV*_*che 6 sql postgresql recursion parent-child common-table-expression

我正在建立一个论坛,非常像 Reddit/Slashdot,即

  • 无限回复嵌套级别
  • 热门评论(按赞/投票排序)会上升到顶部(在他们自己的嵌套/深度级别内),但需要保留树结构(父级始终显示在子级正上方)

这是一个示例表和数据:

DROP TABLE IF EXISTS "comments";
CREATE TABLE comments (
  id BIGINT PRIMARY KEY,
  parent_id BIGINT,
  body TEXT NOT NULL,
  like_score BIGINT,
  depth BIGINT
);

INSERT INTO comments VALUES (  0, NULL, 'Main top of thread post', 5 , 0 );

INSERT INTO comments VALUES (  1, 0, 'comment A', 5 , 1 );
INSERT INTO comments VALUES (  2, 1,    'comment A.A', 3, 2 );
INSERT INTO comments VALUES (  3, 1,    'comment A.B', 1, 2 );
INSERT INTO comments VALUES (  9, 3,    'comment A.B.A', 10, 3 );
INSERT INTO comments VALUES ( 10, 3,    'comment A.B.B', 5, 3 );
INSERT INTO comments VALUES ( 11, 3,    'comment A.B.C', 8, 3 );
INSERT INTO comments VALUES (  4, 1,    'comment A.C', 5, 2 );

INSERT INTO comments VALUES ( 5, 0, 'comment B', 10, 1 );
INSERT INTO comments VALUES ( 6, 5, 'comment B.A', 7, 2 );
INSERT INTO comments VALUES ( 7, 5, 'comment B.B', 5, 2 );
INSERT INTO comments VALUES ( 8, 5, 'comment B.C', 2, 2 );
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止提出的递归查询,但我不知道如何对子项进行排序,但保留树结构(父项应始终高于子项)...

WITH RECURSIVE tree AS (
  SELECT
    ARRAY[]::BIGINT[] AS sortable,
    id,
    body,
    like_score,
    depth
  FROM "comments"
  WHERE parent_id IS NULL

  UNION ALL

  SELECT
    tree.sortable ||  "comments".like_score || "comments".id,
    "comments".id,
    "comments".body,
    "comments".like_score,
    "comments".depth
  FROM "comments", tree
  WHERE "comments".parent_id = tree.id
)
SELECT * FROM tree
ORDER BY sortable DESC
Run Code Online (Sandbox Code Playgroud)

这输出...

+----------------------------------------------------------+
|sortable      |id|body                   |like_score|depth|
+----------------------------------------------------------+
|{10,5,7,6}    |6 |comment B.A            |7         |2    |
|{10,5,5,7}    |7 |comment B.B            |5         |2    |
|{10,5,2,8}    |8 |comment B.C            |2         |2    |
|{10,5}        |5 |comment B              |10        |1    |
|{5,1,5,4}     |4 |comment A.C            |5         |2    |
|{5,1,3,2}     |2 |comment A.A            |3         |2    |
|{5,1,1,3,10,9}|9 |comment A.B.A          |10        |3    |
|{5,1,1,3,8,11}|11|comment A.B.C          |8         |3    |
|{5,1,1,3,5,10}|10|comment A.B.B          |5         |3    |
|{5,1,1,3}     |3 |comment A.B            |1         |2    |
|{5,1}         |1 |comment A              |5         |1    |
|              |0 |Main top of thread post|5         |0    |
+----------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

...但是请注意“评论 B”、“评论 A”和“主题帖子的主要顶部”在他们的孩子下方?如何保持上下文顺序?即我想要的输出是:

+----------------------------------------------------------+
|sortable      |id|body                   |like_score|depth|
+----------------------------------------------------------+
|              |0 |Main top of thread post|5         |0    |
|{10,5}        |5 |comment B              |10        |1    |
|{10,5,7,6}    |6 |comment B.A            |7         |2    |
|{10,5,5,7}    |7 |comment B.B            |5         |2    |
|{10,5,2,8}    |8 |comment B.C            |2         |2    |
|{5,1}         |1 |comment A              |5         |1    |
|{5,1,5,4}     |4 |comment A.C            |5         |2    |
|{5,1,3,2}     |2 |comment A.A            |3         |2    |
|{5,1,1,3}     |3 |comment A.B            |1         |2    |
|{5,1,1,3,10,9}|9 |comment A.B.A          |10        |3    |
|{5,1,1,3,8,11}|11|comment A.B.C          |8         |3    |
|{5,1,1,3,5,10}|10|comment A.B.B          |5         |3    |
+----------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

我实际上希望用户能够通过多种方法进行排序:

  • 人气第一
  • 最不受欢迎的第一个
  • 新的先来
  • 最老的第一
  • 等等

...但在所有情况下,父母都需要显示在他们的孩子之上。但我只是在这里使用“like_score”作为例子,我应该能够从那里找出其余的。

花了很多时间研究网络并自己尝试,感觉我已经接近了,但无法弄清楚最后一部分。

Dav*_*itz 7

1.

tree.sortable ||  -"comments".like_score || "comments".id
                  ^
                 /|\
                  |
                  |  
Run Code Online (Sandbox Code Playgroud)

2.

ORDER BY sortable  
Run Code Online (Sandbox Code Playgroud)
WITH RECURSIVE tree AS (
  SELECT
    ARRAY[]::BIGINT[] AS sortable,
    id,
    body,
    like_score,
    depth
  FROM "comments"
  WHERE parent_id IS NULL

  UNION ALL

  SELECT
    tree.sortable ||  -"comments".like_score || "comments".id,
    "comments".id,
    "comments".body,
    "comments".like_score,
    "comments".depth
  FROM "comments", tree
  WHERE "comments".parent_id = tree.id
)
SELECT * FROM tree
ORDER BY sortable 
Run Code Online (Sandbox Code Playgroud)
+-------------------+----+-------------------------+------------+-------+
| sortable          | id | body                    | like_score | depth |
+-------------------+----+-------------------------+------------+-------+
| (null)            | 0  | Main top of thread post | 5          | 0     |
+-------------------+----+-------------------------+------------+-------+
| {-10,5}           | 5  | comment B               | 10         | 1     |
+-------------------+----+-------------------------+------------+-------+
| {-10,5,-7,6}      | 6  | comment B.A             | 7          | 2     |
+-------------------+----+-------------------------+------------+-------+
| {-10,5,-5,7}      | 7  | comment B.B             | 5          | 2     |
+-------------------+----+-------------------------+------------+-------+
| {-10,5,-2,8}      | 8  | comment B.C             | 2          | 2     |
+-------------------+----+-------------------------+------------+-------+
| {-5,1}            | 1  | comment A               | 5          | 1     |
+-------------------+----+-------------------------+------------+-------+
| {-5,1,-5,4}       | 4  | comment A.C             | 5          | 2     |
+-------------------+----+-------------------------+------------+-------+
| {-5,1,-3,2}       | 2  | comment A.A             | 3          | 2     |
+-------------------+----+-------------------------+------------+-------+
| {-5,1,-1,3}       | 3  | comment A.B             | 1          | 2     |
+-------------------+----+-------------------------+------------+-------+
| {-5,1,-1,3,-10,9} | 9  | comment A.B.A           | 10         | 3     |
+-------------------+----+-------------------------+------------+-------+
| {-5,1,-1,3,-8,11} | 11 | comment A.B.C           | 8          | 3     |
+-------------------+----+-------------------------+------------+-------+
| {-5,1,-1,3,-5,10} | 10 | comment A.B.B           | 5          | 3     |
+-------------------+----+-------------------------+------------+-------+
Run Code Online (Sandbox Code Playgroud)