mysql 父子节点 排序,父子级排序的MySQL树

I'm trying to display a tree structure in MYSQL with this reference (http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/) and I'm seeing that only works with 1 parent in same table.

If you have another table to join with, nothing go well.

Example:

Table Menu:

+----+--------+-------+

| id | name | order |

| 1 | Father | 0 |

| 2 | Father | 1 |

| 3 | Son | 0 |

| 4 | Child | 1 |

| 5 | Granson| 2 |

+----+--------+-------+

Table Relations

+----+---------+-----------+

| id | menu_id | parent_id |

| 1 | 1 | NULL |

| 2 | 2 | NULL |

| 3 | 3 | 1 |

| 4 | 4 | 3 |

| 5 | 5 | 4 |

+----+---------+-----------+

Do the SELECT

SELECT child_menu.*, menu.* FROM menu, relations AS child_menu

WHERE menu.id = child_menu.menu_id

GROUP BY menu_id

I Have this:

+----+--------+-------+

| id | name | order |

| 1 | Father | 0 |

| 2 | Father | 1 |

| 3 | Son | 0 |

| 4 | Child | 1 |

| 5 | Granson| 2 |

+----+--------+-------+

I'm trying to leave their children in order in the same SELECT.

From what I see in the examples, only works if the parent is in the same table.

Can someone help me?

thank you

Edited: EXPECTED OUTPUT:

+----+--------+-------+

| id | name | order |

| 1 | Father | 0 |

| 3 | Son | 0 |

| 4 | Child | 1 |

| 5 | Granson| 2 |

| 2 | Father | 1 |

+----+--------+-------+

I.E. Father

Son

Child

Grandson

解决方案

There are other ways to organize hierarchical data besides the methods shown in Mike Hillyer's blog. I like to use a method I call transitive closure table or closure table for short. In this design, you store every path through the hierarchy, as ancestor/descendant pairs.

create table closure (

ancestor int,

descendant int,

length int,

primary key (ancestor,descendant),

key (descendant,ancestor)

);

insert into closure values

(1,1,0),

(1,3,1),

(1,4,2),

(1,5,3),

(2,2,0),

(3,3,0),

(3,4,1),

(3,5,2),

(4,4,0),

(4,5,1),

(5,5,0);

Note that this set includes even the "paths" of length zero, i.e. a menu item is an "parent" of itself.

Now you can join each menu item m to every its set of ancestors a, by joining to paths where m is the descandant. From there, join back to the menu item o which is in the set of ancestors, and you can access the order.

Use GROUP_CONCAT() to make a string of "breadcrumbs" from the order of each in the chain of ancestors, and this becomes a string you can sort by to get the menu order you want.

SELECT m.*, GROUP_CONCAT(o.`order` ORDER BY a.length DESC) AS breadcrumbs

FROM menu AS m

INNER JOIN closure AS a ON a.descendant = m.id

INNER JOIN menu AS o ON a.ancestor = o.id

GROUP BY m.id

ORDER BY breadcrumbs;

+----+----------+-------+-------------+

| id | name | order | breadcrumbs |

+----+----------+-------+-------------+

| 1 | Father1 | 0 | 0 |

| 3 | Son | 0 | 0,0 |

| 4 | Child | 1 | 0,0,1 |

| 5 | Grandson | 2 | 0,0,1,2 |

| 2 | Father2 | 1 | 1 |

+----+----------+-------+-------------+

Note that the breadcrumbs sort as a string, so if you have some order numbers with 2 or 3 digits, you will get irregular results. Make sure your order numbers all have the same number of digits.

As an alternative, you could simply store the breadcrumbs strings in your original menu table:

ALTER TABLE menu ADD COLUMN breadcrumbs VARCHAR(255);

UPDATE menu SET breadcrumbs = '0,0,1,2' WHERE id = 5;

etc.

Then you can do a simpler query:

SELECT * FROM menu ORDER BY breadcrumbs;

But then it's up to you to manually recalculate all affected breadcrumb strings, if you ever change the order of the menu items.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值