多级父子节点关系

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

Multi-level parent-child nodes relationship

问题

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

  1. select
  2. m.node_id as root,
  3. group_concat(distinct i.node_id order by i.node_id separator ',') as internal,
  4. group_concat(distinct l.node_id order by l.node_id separator ',') as leaf
  5. from craig_test.test m
  6. left join craig_test.test r on m.node_id = r.node_id and r.parent_node_id is null
  7. 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)
  8. 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)
  9. -- where m.parent_node_id is null
  10. 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:

  1. select
  2. m.node_id as root,
  3. group_concat(distinct i.node_id order by i.node_id separator ',') as internal,
  4. group_concat(distinct l.node_id order by l.node_id separator ',') as leaf
  5. from craig_test.test m
  6. left join craig_test.test r on m.node_id = r.node_id and r.parent_node_id is null
  7. 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)
  8. 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)
  9. -- where m.parent_node_id is null
  10. group by 1

答案1

得分: 1

以下是已翻译的内容:

  1. // 构建包括节点、父节点和是否具有子节点的节点列表的递归公共表达式(CTE
  2. WITH RECURSIVE hierarchy (root_node_id, node_id, has_children) AS (
  3. SELECT
  4. t.node_id,
  5. t.node_id,
  6. EXISTS (SELECT 1 FROM test WHERE parent_node_id = t.node_id)
  7. FROM test t
  8. WHERE t.parent_node_id IS NULL
  9. UNION ALL
  10. SELECT
  11. h.root_node_id,
  12. t.node_id,
  13. EXISTS (SELECT 1 FROM test WHERE parent_node_id = t.node_id)
  14. FROM test t
  15. JOIN hierarchy h ON t.parent_node_id = h.node_id
  16. )
  17. SELECT
  18. root_node_id AS root,
  19. GROUP_CONCAT(IF(has_children AND root_node_id <> node_id, node_id, NULL)) AS internal,
  20. GROUP_CONCAT(IF(NOT has_children, node_id, NULL)) AS leaf
  21. FROM hierarchy
  22. 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:

  1. // build list of all nodes with their parent and whether they have children
  2. WITH RECURSIVE hierarchy (root_node_id, node_id, has_children) AS (
  3. SELECT
  4. t.node_id,
  5. t.node_id,
  6. EXISTS (SELECT 1 FROM test WHERE parent_node_id = t.node_id)
  7. FROM test t
  8. WHERE t.parent_node_id IS NULL
  9. UNION ALL
  10. SELECT
  11. h.root_node_id,
  12. t.node_id,
  13. EXISTS (SELECT 1 FROM test WHERE parent_node_id = t.node_id)
  14. FROM test t
  15. JOIN hierarchy h ON t.parent_node_id = h.node_id
  16. )
  17. SELECT
  18. root_node_id AS root,
  19. GROUP_CONCAT(IF(has_children AND root_node_id <> node_id, node_id, NULL)) AS internal,
  20. GROUP_CONCAT(IF(NOT has_children, node_id, NULL)) AS leaf
  21. FROM hierarchy
  22. 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:

确定