获取MySQL父母的所有孩子
我有一个示例 MySQL 表,其中包含列 ID、NAME 和 ID_PARENT。
ID | 名称 | ID_PARENT |
---|---|---|
1 | 节点 1 | 空 |
2 | 节点 2 | 1 |
3 | 节点 3 | 7 |
4 | 节点 4 | 2 |
5 | 节点 5 | 10 |
6 | 节点 6 | 5 |
7 | 节点 7 | 5 |
8 | 节点 8 | 7 |
9 | 节点 9 | 6 |
10 | 节点 10 | 1 |
11 | 节点 | 11空 |
12 | 节点 12 | 11 |
13 | 节点13 | 14 |
14 | NODE 14 | 10 |
18 | NODE 15 | 14 |
如果我想获取节点 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.
ID | NAME | ID_PARENT |
---|---|---|
1 | NODE 1 | NULL |
2 | NODE 2 | 1 |
3 | NODE 3 | 7 |
4 | NODE 4 | 2 |
5 | NODE 5 | 10 |
6 | NODE 6 | 5 |
7 | NODE 7 | 5 |
8 | NODE 8 | 7 |
9 | NODE 9 | 6 |
10 | NODE 10 | 1 |
11 | NODE 11 | NULL |
12 | NODE 12 | 11 |
13 | NODE 13 | 14 |
14 | NODE 14 | 10 |
18 | NODE 15 | 14 |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在关系模型中存储分层数据的“模型”有很多,每种模型都有自己的优点和缺点。
您提出的模型(邻接列表)是最容易理解的,但也是最有限制的。
本文提供了可行解决方案的精彩概述 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.