2017-04-04 29 views
6

我试图创建一个数据库,其中可能有n类别及其子类别。在MySQL/PHP中实现嵌套顺序集

首先,我想创建这样

+-------------+----------------------+--------+ 
| 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; 
+0

您想如何管理的子节点?他们是否成为他们的祖父母的孩子,或只是被父母删除? –

+0

成为祖父母的子女 – jpm

+0

即使意外地删除了根节点,也会发生什么? –

回答

4

这个http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/是你的源代码我想?

你没有使用好查询,这个是add a brother node

你以后add a child node

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:[email protected]+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:[email protected]+1) where id<=(_number+1)*2 ORDER BY id desc ; 
end;