SQL - 嵌套集模型 - 如何找到每个分支的最深父节点(不是叶节点)
给定这样一棵树:
A----B---------C----D | | E----F G | H
我需要找到 C 和 E(每个唯一分支的两个最深的节点;AC 和 AE)
我们的数据库使用嵌套集模型,以及邻接表作为备份以维护树结构,以防万一左右值不同步。
我可以排除所有叶节点( rgt = lft + 1 ) 和根节点( lft = 1 ) 这让我选择了 BE C。正如你可以想象的那样,这是一个大大简化的示例,我们的一些树有超过 100 个节点。如何消除数据中的这些噪音?
这是上面示例的数据(如果它存储在我们的数据库中)。
node | parent | lft | rgt | ------+--------+-----+-----+ A | NULL | 1| 16| B | A | 2| 15| E | B | 3| 8| F | E | 4| 5| H | E | 6| 7| C | B | 9| 14| D | C | 10| 11| G | C | 12| 13|
感谢您的帮助!
Given a tree like this:
A----B---------C----D | | E----F G | H
I need to find C and E (the two deepest nodes of each unique branch; A-C and A-E)
Our database uses the nested set model, as well as the adjacency list as a backup to maintain tree structure in case the left and right values get out of synch.
I can exclude all leaf nodes ( rgt = lft + 1 )
and the root node ( lft = 1 )
which leaves me with B E C. As you can imagine this is a greatly simplified example, some of our trees have over 100 nodes. How can I get rid of this noise in my data?
Here's the data for the example above if it were stored in our database.
node | parent | lft | rgt | ------+--------+-----+-----+ A | NULL | 1| 16| B | A | 2| 15| E | B | 3| 8| F | E | 4| 5| H | E | 6| 7| C | B | 9| 14| D | C | 10| 11| G | C | 12| 13|
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的,第一步是通过叶子节点的属性 right 等于 left + 1 来识别叶子节点。下一步是找到这些叶子节点的所有父节点:这些叶子节点的 left 小于叶子,而 right 大于叶子的那个。最后一步是排除除左值最大(即最接近叶节点)之外的所有父节点。
T-SQL 中的一个示例,其中 l 是叶节点,p1 是我们要查找的直接父节点,p2 用于清除所有非直接父节点。
首先设置一个包含示例数据的测试表:
下面是检索您请求的节点 C 和 E 的查询:
You're right, the first step is to identify the leaf nodes through their property right equals left + 1. Next step is to find all parent nodes for those leaf nodes: these have a left smaller than that of the leaf and right larger than that of the leaf. And the final step is to exclude all parents but the one that has the largest left value (i.e. is closest to the leaf node).
An example in T-SQL, where l is the leaf node, p1 is the direct parent we're looking for and p2 is used to weed out all the non-direct parents.
First set up a test table with your example data in it:
And here's the query that retrieves the both nodes C and E you requested: