PGSQL 的注释树

发布于 2025-01-10 15:21:45 字数 1492 浏览 0 评论 0原文

我的评论树有一个简单的评论示例:

1. comment_id_1
    - comment_id_3
        * commnet_id_5
        * commnet_id_6
    - comment_id_4
        * commnet_id_7
        * commnet_id_8
2. comment_id_2
    - comment_id_9
    - comment_id_10
id | parent_id
---+------------
1  |  null
2  |  null
3  |  1
4  |  1
5  |  3
6  |  3
7  |  4
8  |  4
9  |  2
10 |  2

我想获取给定评论或给定评论列表的所有子评论,从 parent_id = 1content_id = 1 开始>。我进行了查询,但失败了。

WITH RECURSIVE sub_comments AS (
    SELECT com.parent_id, com.id
    FROM comments com
    UNION
    SELECT c.parent_id, c.id
    FROM comments c
         JOIN sub_comments sc ON c.id = sc.parent_id
)
SELECT *
FROM sub_comments
WHERE parent_id IS NOT NULL

我需要添加什么才能仅选择一个(或多个)特定“分支”评论?

我的失败:

WITH RECURSIVE sub_comments AS (
    SELECT com.parent_id, com.id
    FROM comments com
    WHERE com.parent_id = 1
    UNION
    SELECT c.parent_id, c.id
    FROM comments c
         JOIN sub_comments sc ON c.id = sc.parent_id
)
SELECT *
FROM sub_comments
WHERE comment_id IS NOT NULL

替代解决方案是添加这样的限制,但我找不到用条件限制的方法。

WITH RECURSIVE sub_comments AS (
    SELECT com.parent_id, com.id
    FROM comments com
    UNION
    SELECT c.parent_id, c.id
    FROM comments c
         JOIN sub_comments sc ON c.id = sc.parent_id
)
SELECT *
FROM sub_comments
LIMIT 4 (but limit should count only if parent_it IS NULL)

There is simply example of comments for my comments tree:

1. comment_id_1
    - comment_id_3
        * commnet_id_5
        * commnet_id_6
    - comment_id_4
        * commnet_id_7
        * commnet_id_8
2. comment_id_2
    - comment_id_9
    - comment_id_10
id | parent_id
---+------------
1  |  null
2  |  null
3  |  1
4  |  1
5  |  3
6  |  3
7  |  4
8  |  4
9  |  2
10 |  2

And I want to get all sub-comments of given comment or given comments list, starts from parent_id = 1 and content_id = 1. I made query but it fails.

WITH RECURSIVE sub_comments AS (
    SELECT com.parent_id, com.id
    FROM comments com
    UNION
    SELECT c.parent_id, c.id
    FROM comments c
         JOIN sub_comments sc ON c.id = sc.parent_id
)
SELECT *
FROM sub_comments
WHERE parent_id IS NOT NULL

What do I need to add to select only one (or more) particulary "branch" of comments?

My fails:

WITH RECURSIVE sub_comments AS (
    SELECT com.parent_id, com.id
    FROM comments com
    WHERE com.parent_id = 1
    UNION
    SELECT c.parent_id, c.id
    FROM comments c
         JOIN sub_comments sc ON c.id = sc.parent_id
)
SELECT *
FROM sub_comments
WHERE comment_id IS NOT NULL

Alternate solution is to add such of limit but I coludn't find way to limit with condition.

WITH RECURSIVE sub_comments AS (
    SELECT com.parent_id, com.id
    FROM comments com
    UNION
    SELECT c.parent_id, c.id
    FROM comments c
         JOIN sub_comments sc ON c.id = sc.parent_id
)
SELECT *
FROM sub_comments
LIMIT 4 (but limit should count only if parent_it IS NULL)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2025-01-17 15:21:45

您的第二次尝试很接近,但您翻转了连接条件。查询树时,从根节点开始,然后重复将子节点添加到结果集中,直到结果集中不再有具有父节点但本身尚未包含在结果集中的行。

WITH RECURSIVE sub_comments AS (
    SELECT com.parent_id, com.id
    FROM comments com
    WHERE com.id = 1 -- start with the root itself, not its children
  UNION
    SELECT c.parent_id, c.id
    FROM comments c
    JOIN sub_comments sc ON c.parent_id = sc.id -- or: WHERE c.parent_id IN (SELECT id FROM sub_comments)
)
SELECT *
FROM sub_comments

Your second attempt comes close, but you flipped the join condition. When querying for the tree, you start with the root node and then repeatedly add children to the result set, until there are no rows left that have a parent in the result set but are not yet in the result set themselves.

WITH RECURSIVE sub_comments AS (
    SELECT com.parent_id, com.id
    FROM comments com
    WHERE com.id = 1 -- start with the root itself, not its children
  UNION
    SELECT c.parent_id, c.id
    FROM comments c
    JOIN sub_comments sc ON c.parent_id = sc.id -- or: WHERE c.parent_id IN (SELECT id FROM sub_comments)
)
SELECT *
FROM sub_comments
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文