PostgreSQL递归和ORDER BY最新的根

时间:2013-02-01 15:25:29

标签: postgresql recursive-query

我需要一个分级评论,我需要按最新的根评论排序。

我有这张桌子

    CREATE TABLE comment
(
  id serial PRIMARY KEY,
  text text NOT NULL,
  parent_id integer REFERENCES comment(id),
  date timestamp
);

INSERT INTO comment (id, text, parent_id, date) VALUES (1, 'First Root Comment', NULL, '2013-01-02 20:00:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (2, 'Second Root Comment', NULL, '2013-01-02 20:20:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (3, 'Reply 1 to Root Comment', 1, '2013-01-02 20:01:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (4, 'Reply 2 to Reply 1', 1, '2013-01-02 20:02:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (5, 'Reply 1 to Second Root Comment', 2, '2013-01-02 20:21:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (6, 'Reply 3 to Reply 1', 1, '2013-01-02 20:03:00');
INSERT INTO comment (id, text, parent_id, date) VALUES (7, 'Reply 1 to Reply 2', 4, '2013-01-02 20:02:30');

此代码显示分层 - http://www.sqlfiddle.com/#!12/96b37/2

WITH RECURSIVE cte (id, text, date, path, parent_id, depth)  AS (

    SELECT  id,
        text,
        date,
        array[id] AS path,
        parent_id,
        1 AS depth
    FROM    comment
    WHERE   parent_id IS NULL


    UNION ALL

    SELECT  comment.id,
        comment.text,
        comment.date,
        cte.path || comment.id,
        comment.parent_id,
        cte.depth + 1 AS depth
    FROM    comment
    JOIN cte ON comment.parent_id = cte.id
    )
    SELECT id, text, date, path, depth FROM cte
    ORDER BY path;

结果就是

Firt root comment (older root comment)
- Reply 1 to First Root Comment
- Reply 2 to First Root Comment
-- Reply 1 to Reply 2
- Reply 3 to First Root Comment
Second Root Comment (newest root comment)
- Reply 1 to Second Root Comment

但我想要这个结果(按最新根评论排序)

Second Root Comment (newest root comment)
- Reply 1 to Second Root Comment
Firt root comment (older root comment)
- Reply 1 to First Root Comment
- Reply 2 to First Root Comment
-- Reply 1 to Reply 2
- Reply 3 to First Root Comment

有什么想法吗?感谢

1 个答案:

答案 0 :(得分:2)

您需要某种根注释的标识符,以便您可以对它们进行排序。怎么样:

WITH RECURSIVE cte (id, text, date, path, parent_id, depth)  AS (
SELECT  id,
    text,
    date,
    array[id] AS path,
    parent_id,
    1 AS depth,
    id as root
FROM    comment
WHERE parent_id IS NULL
UNION ALL
SELECT  comment.id,
    comment.text,
    comment.date,
    cte.path || comment.id,
    comment.parent_id,
    cte.depth + 1 AS depth,
    root
FROM    comment
JOIN cte ON comment.parent_id = cte.id
)
SELECT id, text, date, path, depth FROM cte
ORDER BY root desc, path;