获取MySQL父母的所有孩子

发布于 2025-01-13 00:24:43 字数 2402 浏览 1 评论 0原文

我有一个示例 MySQL 表,其中包含列 ID、NAMEID_PARENT

ID名称ID_PARENT
1节点 1
2节点 21
3节点 37
4节点 42
5节点 510
6节点 65
7节点 75
8节点 87
9节点 96
10节点 101
11节点11空
12节点 1211
13节点1314
14NODE 1410
18NODE 1514

如果我想获取节点 1 的子节点,那么我将得到以下内容。

     NODE 1
    /      \
NODE 2     NODE 10
  |          |
NODE 4       |__________________
             |                 |
           NODE 5              |
            /   \              |
       NODE 6   NODE 7       NODE 14
         |         |        /     \
       NODE 9   NODE 3  NODE 15   NODE 13                                                     

如果你认为为什么 NODE 7 是 NODE 3 的父节点。答案是我们可以在创建所有节点后修改父节点。

我正在使用以下 SQL 查询,但它无法获取所有节点。但是,如果 id_parent 数据在表中按升序排列,那么它就可以正常工作。

SELECT id,
       node_name,
       id_parent
FROM   tree
WHERE  id=1
UNION
SELECT id,
       node_name,
       id_parent
FROM   (
                SELECT   id,
                         node_name,
                         id_parent
                FROM     tree t
                ORDER BY t.id_parent ) tree,
       (
              SELECT @pv := 1) initialisation
WHERE  Find_in_set(id_parent, @pv) > 0
AND    @pv := concat(@pv, ',', id);

通过运行上面的 SQL,我得到以下节点。

     NODE 1
    /      \
NODE 2     NODE 10
  |          |
NODE 4       |___________
             |          |
           NODE 5       |
                        |
                      NODE 14
                      /     \
                  NODE 15   NODE 13                                                        

那么,如何修改查询以便获得第一个 NODES 表示中演示的所有节点?

I have a sample MySQL table with the columns ID, NAME AND ID_PARENT.

IDNAMEID_PARENT
1NODE 1NULL
2NODE 21
3NODE 37
4NODE 42
5NODE 510
6NODE 65
7NODE 75
8NODE 87
9NODE 96
10NODE 101
11NODE 11NULL
12NODE 1211
13NODE 1314
14NODE 1410
18NODE 1514

If I want to get the children of Node 1 then I will get the following.

     NODE 1
    /      \
NODE 2     NODE 10
  |          |
NODE 4       |__________________
             |                 |
           NODE 5              |
            /   \              |
       NODE 6   NODE 7       NODE 14
         |         |        /     \
       NODE 9   NODE 3  NODE 15   NODE 13                                                     

If you think why the NODE 7 is the parent of NODE 3. The answer is that we can modify parents after the creation of all nodes.

I am using the following SQL query and it cannot get all the nodes. But, if the id_parent data is in asc order within the table then it works fine.

SELECT id,
       node_name,
       id_parent
FROM   tree
WHERE  id=1
UNION
SELECT id,
       node_name,
       id_parent
FROM   (
                SELECT   id,
                         node_name,
                         id_parent
                FROM     tree t
                ORDER BY t.id_parent ) tree,
       (
              SELECT @pv := 1) initialisation
WHERE  Find_in_set(id_parent, @pv) > 0
AND    @pv := concat(@pv, ',', id);

By running the above SQL I get the following nodes.

     NODE 1
    /      \
NODE 2     NODE 10
  |          |
NODE 4       |___________
             |          |
           NODE 5       |
                        |
                      NODE 14
                      /     \
                  NODE 15   NODE 13                                                        

So, how can I modify the query so that I get all the nodes as demonstrated in the first NODES representation?

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

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

发布评论

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

评论(1

夏了南城 2025-01-20 00:24:43

在关系模型中存储分层数据的“模型”有很多,每种模型都有自己的优点和缺点。

您提出的模型(邻接列表)是最容易理解的,但也是最有限制的。

本文提供了可行解决方案的精彩概述 https://www.databasestar.com/hierarchical- data-sql/ 这样您就可以权衡每种方法是否足以满足您的要求。

There are quite a few 'models' of storing the hierarchical data in relational model, each with it's own pros and cons.

The model you've presented (the adjacency list) is the easiest to understand, but also the most limiting.

This article offers a great overview of viable solutions https://www.databasestar.com/hierarchical-data-sql/ so you can weight the adequacy of each approach to your requirements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文