如何根据树遍历算法从该结果集中生成树视图?
我有这张表:
CREATE TABLE `categories` (
`id` int(11) NOT NULL auto_increment,
`category_id` int(11) default NULL,
`root_id` int(11) default NULL,
`name` varchar(100) collate utf8_unicode_ci NOT NULL,
`lft` int(11) NOT NULL,
`rht` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `category_id` (`category_id`),
KEY `lft` (`lft`,`rht`),
KEY `root_id` (`root_id`)
)
基于这个问题: 获取修改后的先序树遍历模型(嵌套集)到
区别在于我在一张表中有很多树。每行都有一个代表其父级及其顶级父级的外键:category_id 和 root_id。我还有基于此示例的 lft 和 rht 字段: http://articles.sitepoint.com/article/hierarchical-data-database/ 2
基于此行:
INSERT INTO `categories` VALUES(1, NULL, NULL, 'Fruits', 1, 14);
INSERT INTO `categories` VALUES(2, 1, 1, 'Apple', 2, 3);
INSERT INTO `categories` VALUES(3, 1, 1, 'Orange', 4, 9);
INSERT INTO `categories` VALUES(4, 3, 1, 'Orange Type 1', 5, 6);
INSERT INTO `categories` VALUES(5, 3, 1, 'Orange Type 2', 7, 8);
INSERT INTO `categories` VALUES(6, 1, 1, 'Pear', 10, 11);
INSERT INTO `categories` VALUES(7, 1, 1, 'Banana', 12, 13);
INSERT INTO `categories` VALUES(8, NULL, NULL, 'Eletronics', 1, 14);
INSERT INTO `categories` VALUES(9, 8, 8, 'Cell Phones', 2, 3);
INSERT INTO `categories` VALUES(10, 8, 8, 'Computers', 4, 9);
INSERT INTO `categories` VALUES(11, 10, 8, 'PC', 5, 6);
INSERT INTO `categories` VALUES(12, 10, 8, 'MAC', 7, 8);
INSERT INTO `categories` VALUES(13, 8, 8, 'Printers', 10, 11);
INSERT INTO `categories` VALUES(14, 8, 8, 'Cameras', 12, 13);
如何构建表示这棵树的有序列表?
使用下面的sql:
SELECT c. * , (COUNT( p.id ) -1) AS depth
FROM `categorias` AS p
CROSS JOIN categories AS c
WHERE (
c.lft
BETWEEN p.lft
AND p.rht
)
GROUP BY c.id
ORDER BY c.lft;
我得到了这个结果:
正如你所看到的,我也需要按 root_id 订购,所以我可以生成正确的树。
另外,获得树后,有没有办法按名称对每个节点进行排序?
I have this table:
CREATE TABLE `categories` (
`id` int(11) NOT NULL auto_increment,
`category_id` int(11) default NULL,
`root_id` int(11) default NULL,
`name` varchar(100) collate utf8_unicode_ci NOT NULL,
`lft` int(11) NOT NULL,
`rht` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `category_id` (`category_id`),
KEY `lft` (`lft`,`rht`),
KEY `root_id` (`root_id`)
)
Based on this question:
Getting a modified preorder tree traversal model (nested set) into a <ul>
The difference is that I have many trees in one table. Each row has a foreign key representing its parent and its top parent: category_id and root_id. Also I have the lft and rht fields based on this example:
http://articles.sitepoint.com/article/hierarchical-data-database/2
Based on this rows:
INSERT INTO `categories` VALUES(1, NULL, NULL, 'Fruits', 1, 14);
INSERT INTO `categories` VALUES(2, 1, 1, 'Apple', 2, 3);
INSERT INTO `categories` VALUES(3, 1, 1, 'Orange', 4, 9);
INSERT INTO `categories` VALUES(4, 3, 1, 'Orange Type 1', 5, 6);
INSERT INTO `categories` VALUES(5, 3, 1, 'Orange Type 2', 7, 8);
INSERT INTO `categories` VALUES(6, 1, 1, 'Pear', 10, 11);
INSERT INTO `categories` VALUES(7, 1, 1, 'Banana', 12, 13);
INSERT INTO `categories` VALUES(8, NULL, NULL, 'Eletronics', 1, 14);
INSERT INTO `categories` VALUES(9, 8, 8, 'Cell Phones', 2, 3);
INSERT INTO `categories` VALUES(10, 8, 8, 'Computers', 4, 9);
INSERT INTO `categories` VALUES(11, 10, 8, 'PC', 5, 6);
INSERT INTO `categories` VALUES(12, 10, 8, 'MAC', 7, 8);
INSERT INTO `categories` VALUES(13, 8, 8, 'Printers', 10, 11);
INSERT INTO `categories` VALUES(14, 8, 8, 'Cameras', 12, 13);
How can I build an ordened list representing this tree?
With the sql bellow:
SELECT c. * , (COUNT( p.id ) -1) AS depth
FROM `categorias` AS p
CROSS JOIN categories AS c
WHERE (
c.lft
BETWEEN p.lft
AND p.rht
)
GROUP BY c.id
ORDER BY c.lft;
I got this result:
As you can see, I need to order by root_id too, so that I can generate the correct tree.
Also, after get the tree, is there a way to order each node by name?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构建嵌套树模型时,切勿在
lft
和rgt
上重复。事实上,您应该声明它们是唯一的。在您的数据模型中,类别
1
和8
的集合重叠。假设,1
到14
均用于项目1
和8
。将它们替换为以下值:
现在您不必在
root_id
上订购。没有简单的方法,除非您从头开始按名称顺序插入节点。具有较大
name
的同级应该具有较大的lft
和rgt
:嵌套树只能有一个隐式顺序。
MySQL 中还有一种查询邻接表的方法:
,但是,如果您想对
id
以外的任何内容进行排序,则必须创建一个额外的唯一排序列。您可能还想阅读这篇文章:
展示了如何更有效地存储和查询嵌套集。
When building the nested tree model, never make duplicates on
lft
andrgt
. In fact, you should declare them unique.In you data model, the sets for category
1
and8
overlap. Say,1
to14
are used both for items1
and8
.Replace them with these values:
Now you don't have to order on
root_id
.No easy way, unless you insert the nodes in the name order from the beginning. Siblings with the greater
name
should have greaterlft
andrgt
:A nested tree can only have one implicit order.
There is also a way to query adjacency list in
MySQL
:, however, you will have to create an additional unique ordering column if you want to order on anything else than
id
.You may also want to read this article:
which shows how to store and query nested sets more efficiently.
我得到了它。
您需要做的就是将 root_id 也设置为顶级父级,以便您可以正确排序。
通过下面的查询,我可以拥有分离的树,并且仅更新我正在处理的树:
I got it.
All you need to do is set root_id to the top parents too, so that you can ORDER BY correctly.
With the query bellow I can have separeted trees, and uptade only the tree that I'm working on: