我认为,类似的方法应该可以解决问题:
SELECt * FROM(SELECt n.id, n.val, CONNECT_BY_ISLEAF isleaf FROM NODES n LEFT JOIN RELATION r ON n.id = r.id_childConNECT BY PRIOR n.id = r.id_fatherSTART WITH r.id_father IS NULL)WHERe isleaf = 1
哦,顺便说一句,您无需使用层级查询就可以获取所有叶子。只需从关系表中选择所有节点,这些节点都不是父节点。像这样的东西:
SELECt n.* FROM NODES nWHERe NOT EXISTS (SELECt ID_FATHER FROM RELATION r WHERe r.id_father = n.id)
为了从指定的节点获取叶节点,只需更改START WITH子句中的条件,以从您感兴趣的节点开始反向进行树。例如,此查询将返回id = 5的节点的所有子叶:
SELECt * FROM(SELECt n.id, n.val, CONNECT_BY_ISLEAF isleaf FROM NODES n LEFT JOIN RELATION r ON n.id = r.id_childConNECT BY PRIOR n.id = r.id_fatherSTART WITH n.id = 5)WHERe isleaf = 1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)