Depth in MYSQL and Closure Table Trees

前端 未结 1 479
春和景丽
春和景丽 2021-02-04 17:46

How would I go about populating a closure table\'s depth/length column when inserting a new node to the tree?

The values in ancestor and descendant are IDs from another

1条回答
  •  礼貌的吻别
    2021-02-04 18:36

    Add depth+1 to the first SELECT.

    INSERT INTO closure_tree_path (ancestor, descendant, depth)
    SELECT ancestor, '{$node_id}', depth+1 FROM closure_tree_path
    WHERE descendant = '{$parent_id}'
    UNION ALL SELECT '{$node_id}', '{$node_id}', 0;
    

    0 讨论(0)
提交回复
热议问题