【问题标题】:Implementing Nested Order Set in MySQL/PHP在 MySQL/PHP 中实现嵌套顺序集
【发布时间】:2017-04-04 07:40:10
【问题描述】:

我正在尝试创建一个数据库,其中可能有n 个类别及其子类别。

首先我尝试像这样创建邻接模型数据库

+-------------+----------------------+--------+
| category_id | name                 | parent |
+-------------+----------------------+--------+
|           1 | Electronics          |   NULL |
|           2 | Mobile               |      1 |
|           3 | Washing Machine      |      1 |
|           4 | Samsung              |      2 |
+-------------+----------------------+--------+

但是,我在删除节点时遇到了一个问题,例如如何管理已删除节点的子节点等。

然后我正在尝试实现由Joe Celko 设置的嵌套订单集

每个图中的表结构:

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;

【问题讨论】:

  • 你想如何管理子节点?他们是成为祖父母的孩子还是被父母一起删除?
  • 成为祖父母的孩子
  • 如果根节点被删除,即使是意外删除,会发生什么?
  • 所有孩子...n也将被删除。
  • @jpm 这个问题你解决了吗?

标签: mysql sql algorithm tree hierarchical-data


【解决方案1】:

我认为这个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;

【讨论】:

    【解决方案2】:
    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;
    

    【讨论】:

      猜你喜欢
      • 2013-01-04
      • 1970-01-01
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      相关资源
      最近更新 更多