我最近一直在使用
Nested Set Model的废话.我喜欢为每一个有用的操作和视图设计查询.有一件事我坚持是如何选择一个节点的直接孩子(而且只有孩子,而不是后裔!).
解决方法
你看过你发贴的文章吗?它在“查找节点的直接下属”标题下
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 = 'PORTABLE ELECTRONICS' 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;
但是,我做的(这是作弊)是将嵌套集合与邻接列表相结合 – 我在表中嵌入了一个“parent_id”,所以我可以轻松地询问一个节点的子节点.