在MySQL/PHP中实现嵌套集合排序

6
我将尝试创建一个数据库,其中可以有n个类别及其子类别。
首先,我尝试创建这样的邻接模型数据库。
+-------------+----------------------+--------+
| category_id | name                 | parent |
+-------------+----------------------+--------+
|           1 | Electronics          |   NULL |
|           2 | Mobile               |      1 |
|           3 | Washing Machine      |      1 |
|           4 | Samsung              |      2 |
+-------------+----------------------+--------+

但是,当我删除一个节点时,我面临一个问题,例如如何管理已删除节点的子节点等。
然后,我尝试通过Joe Celko实现嵌套有序集。 sample_structure 每个图中的表结构:
Figure 1:
+----+-------------+-----+-----+
| id | name        | lft | rgt |
+----+-------------+-----+-----+
| 1  | Electronics | 1   | 2   |
+----+-------------+-----+-----+

Figure 2:
+----+-------------+-----+-----+
| id | name        | lft | rgt |
+----+-------------+-----+-----+
| 1  | Electronics | 1   | 4   |
+----+-------------+-----+-----+
| 2  | Mobile      | 2   | 3   |
+----+-------------+-----+-----+

Figure 3:
+----+-----------------+-----+-----+
| id | name            | lft | rgt |
+----+-----------------+-----+-----+
| 1  | Electronics     | 1   | 6   |
+----+-----------------+-----+-----+
| 2  | Mobile          | 2   | 3   |
+----+-----------------+-----+-----+
| 3  | Washing Machine | 4   | 5   |
+----+-----------------+-----+-----+

Figure 4:
+----+-----------------+-----+-----+
| id | name            | lft | rgt |
+----+-----------------+-----+-----+
| 1  | Electronics     | 1   | 8   |
+----+-----------------+-----+-----+
| 2  | Mobile          | 2   | 5   |
+----+-----------------+-----+-----+
| 3  | Washing Machine | 6   | 7   |
+----+-----------------+-----+-----+
| 4  | Samsung         | 3   | 4   |
+----+-----------------+-----+-----+

但我无法插入具有正确 rgtlft 的新节点。我使用了这个,但它没有生成正确的 rgtlft 值。

LOCK TABLE nested_category WRITE;

SELECT @myRight := rgt FROM nested_category
WHERE name = 'Mobile';

UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myRight;
UPDATE nested_category SET lft = lft + 2 WHERE lft > @myRight;

INSERT INTO nested_category(name, lft, rgt) VALUES('LG', @myRight + 1, @myRight + 2);

UNLOCK TABLES;

你想如何管理子节点?它们会成为其祖父母的子节点,还是随其父节点一起被删除? - Harsh Gupta
成为祖父母的孙子女。 - jpm
如果根节点被意外删除,应该发生什么? - Harsh Gupta
所有子文件夹和它们的内容都将被删除。 - jpm
@jpm,你解决这个问题了吗? - Blag
2个回答

4
这个http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/是您的源代码吗?
您没有使用正确的查询,这个是“添加兄弟节点”的查询。
您需要的是“添加子节点”的查询:
LOCK TABLE nested_category WRITE;

SELECT @myLeft := lft FROM nested_category
WHERE name = 'Mobile';

UPDATE nested_category SET rgt = rgt + 2 WHERE rgt > @myLeft;
UPDATE nested_category SET lft = lft + 2 WHERE lft > @myLeft;

INSERT INTO nested_category(name, lft, rgt) VALUES('LG', @myLeft + 1, @myLeft + 2);

UNLOCK TABLES;

0
CREATE PROCEDURE nested_insert(_name VARCHAR(45))
BEGIN
DECLARE _number int;
set @var=0;

-- first insert paramater value(_name) into nested_table
INSERT into nested_table(name) VALUE(_name);

-- count the total row  value from nested table;
SELECT count(*) from  nested_table into _number;

-- first update the all lft column from top to button by varibale with increment
UPDATE nested_table set lft=(@var:=@var+1) where id <=_number;

-- second update the all rgt column from button to top by varibale with increment in descending order id
UPDATE nested_table set rgt=(@var:=@var+1) where id<=(_number+1)*2 ORDER BY id desc ;
end;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接