Php MYSQL和闭包表树中的深度
在树中插入新节点时,如何填充闭包表的深度/长度列 “祖先”和“后代”中的值是来自另一个表的ID,表示要在树结构中排列的页面 闭合表:Php MYSQL和闭包表树中的深度,php,mysql,transitive-closure-table,Php,Mysql,Transitive Closure Table,在树中插入新节点时,如何填充闭包表的深度/长度列 “祖先”和“后代”中的值是来自另一个表的ID,表示要在树结构中排列的页面 闭合表: ancestor descendant depth 1 1 0 1 2 1 1 3 1 1 4 1 2 2
ancestor descendant depth
1 1 0
1 2 1
1 3 1
1 4 1
2 2 0
3 3 0
4 4 0
这将正确插入祖先和后代,但我不确定如何填充深度列
插入查询:
INSERT INTO closure_tree_path (ancestor, descendant)
SELECT ancestor, '{$node_id}' FROM closure_tree_path
WHERE descendant = '{$parent_id}'
UNION ALL SELECT '{$node_id}', '{$node_id}';
最好的办法是什么?非常感谢 向第一个选择添加深度+1
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;
我在SQL fiddle中试用过,它看起来可以正常工作?(减去最后缺少的额外“0”)。应该一开始就把它放在小提琴上,以便抓住所有的错误。