我正在使用嵌套集将数据库存储在数据库中.表的字段是id,lft,rgt和name.
给定一个节点ID,我需要找到它们本身就是叶节点的所有直接子节点(不是孙子节点).
文章在MySQL管理层次数据给出了如何使用嵌套集合一个很好的例子,并给出了许多常见的查询,包括这一个例子.
这是如何找到节点的直接子节点:
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth FROM nested_category AS node, nested_category AS parent, nested_category AS sub_parent, ( SELECT node.name, (COUNT(parent.name) - 1) AS depth FROM nested_category AS node, nested_category AS parent WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.name = '**[[MY NODE]]**' GROUP BY node.name ORDER BY node.lft )AS sub_tree WHERE node.lft BETWEEN parent.lft AND parent.rgt AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt AND sub_parent.name = sub_tree.name GROUP BY node.name HAVING depth = 1 ORDER BY node.lft;
然后将它与叶子节点rgt
等于的事实相结合,然后进行lft + 1
设置.原谅双关语.