多级父子节点关系

huangapple go评论65阅读模式
英文:

Multi-level parent-child nodes relationship

问题

以下是您要翻译的代码部分的内容:

select
	m.node_id as root,
	group_concat(distinct i.node_id order by i.node_id separator ',') as internal,
	group_concat(distinct l.node_id order by l.node_id separator ',') as leaf
from craig_test.test m
left join craig_test.test r on m.node_id = r.node_id and r.parent_node_id is null
left join craig_test.test i on m.node_id = i.node_id and i.node_id in (select parent_node_id from craig_test.test where parent_node_id is not null)
left join craig_test.test l on m.node_id = l.node_id and l.node_id not in (select parent_node_id from craig_test.test where parent_node_id is not null)
-- where m.parent_node_id is null
group by 1

如果您需要进一步的翻译或有其他问题,请随时提出。

英文:

I have an original table with 2 columns, parent_node_id (parent) and node_id (child) and I want to get a final output of 3 columns: "root", "internal" and "leaf" nodes. I want to use group_concat() and group by to achieve the expected output in the picture provided.

This is the original table from the tree diagram in the picture provided.

parent_node_id node_id
NULL 10
NULL 20
10 1
10 2
2 3
2 4
20 5
20 6
6 7
6 8
8 9

question case

I need to get an output that looks like this:

root internal leaf
10 2 1,3,4
20 6,8 5,7,9

I tried creating 3 CTEs to get individual root, internal and leaf nodes. However, I am unable to create a sub relational table that gives the relationship with the parent and root nodes.

Here is what i tried:

    select
	m.node_id as root,
    group_concat(distinct i.node_id order by i.node_id separator ',') as internal,
    group_concat(distinct l.node_id order by l.node_id separator ',') as leaf
from craig_test.test m
left join craig_test.test r on m.node_id = r.node_id and r.parent_node_id is null
left join craig_test.test i on m.node_id = i.node_id and i.node_id in (select parent_node_id from craig_test.test where parent_node_id is not null)
left join craig_test.test l on m.node_id = l.node_id and l.node_id not in (select parent_node_id from craig_test.test where parent_node_id is not null)
-- where m.parent_node_id is null
group by 1

答案1

得分: 1

以下是已翻译的内容:

// 构建包括节点、父节点和是否具有子节点的节点列表的递归公共表达式(CTE
WITH RECURSIVE hierarchy (root_node_id, node_id, has_children) AS (
    SELECT
        t.node_id,
        t.node_id,
        EXISTS (SELECT 1 FROM test WHERE parent_node_id = t.node_id)
	FROM test t
    WHERE t.parent_node_id IS NULL

    UNION ALL

    SELECT
        h.root_node_id,
        t.node_id,
        EXISTS (SELECT 1 FROM test WHERE parent_node_id = t.node_id)
	FROM test t
    JOIN hierarchy h ON t.parent_node_id = h.node_id
)
SELECT
    root_node_id AS root,
    GROUP_CONCAT(IF(has_children AND root_node_id <> node_id, node_id, NULL)) AS internal,
    GROUP_CONCAT(IF(NOT has_children, node_id, NULL)) AS leaf
FROM hierarchy
GROUP BY root_node_id;

CTE构建的结果如下:

root_node_id node_id has_children
10 10 1
20 20 1
10 1 0
10 2 1
20 5 0
20 6 1
10 3 0
10 4 0
20 7 0
20 8 1
20 9 0

然后,GROUP BY 查询的输出如下:

root internal leaf
10 2 1,3,4
20 6,8 5,7,9
英文:

There are more efficient ways of doing this, but this will provide the result you requested:

// build list of all nodes with their parent and whether they have children
WITH RECURSIVE hierarchy (root_node_id, node_id, has_children) AS (
    SELECT
        t.node_id,
        t.node_id,
        EXISTS (SELECT 1 FROM test WHERE parent_node_id = t.node_id)
	FROM test t
    WHERE t.parent_node_id IS NULL

    UNION ALL

    SELECT
        h.root_node_id,
        t.node_id,
        EXISTS (SELECT 1 FROM test WHERE parent_node_id = t.node_id)
	FROM test t
    JOIN hierarchy h ON t.parent_node_id = h.node_id
)
SELECT
    root_node_id AS root,
    GROUP_CONCAT(IF(has_children AND root_node_id <> node_id, node_id, NULL)) AS internal,
    GROUP_CONCAT(IF(NOT has_children, node_id, NULL)) AS leaf
FROM hierarchy
GROUP BY root_node_id;

The CTE builds:

root_node_id node_id has_children
10 10 1
20 20 1
10 1 0
10 2 1
20 5 0
20 6 1
10 3 0
10 4 0
20 7 0
20 8 1
20 9 0

And then the GROUP BY query outputs:

root internal leaf
10 2 1,3,4
20 6,8 5,7,9

huangapple
  • 本文由 发表于 2023年3月15日 18:57:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定