MySQL:查找子树的所有叶节点

时间:2015-08-19 11:17:28

标签: php mysql tree adjacency-list adjacency-list-model

我有一个存储在mySQL表中的类别的树结构,其category_idparent_id关系。 Parent_id = Null对应于根节点。

Category (category_id, category_name, parent_id)

我要做的是获取所有叶子节点给出节点的category_id。我跟着this文章。它讨论了使用以下查询获取所有叶节点:

SELECT t1.category_name FROM
category AS t1 LEFT JOIN category as t2
ON t1.category_id = t2.parent_id
WHERE t2.category_id IS NULL;

但我正在尝试获取子树的叶节点。例如:

enter image description here

在给定节点3的上述结构中,结果将是:9,10,7,11,12,13。

我也尝试过这里给出的解决方案:adjacency model , given an id return the leaf nodes。但是我无法获得理想的结果。

你能帮我找一个解决方案吗?

3 个答案:

答案 0 :(得分:1)

哦,好吧......我碰巧找到了解决办法......然而,这有点尴尬:

SELECT TRIM(RIGHT(TRIM(concat_ws(' ',
ifnull(t1.category_id,''),
ifnull(t2.category_id,''),
ifnull(t3.category_id,''),
ifnull(t4.category_id,'')
)),2)) AS leaf_node
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent_category = t1.category_id
LEFT JOIN category AS t3 ON t3.parent_category = t2.category_id
LEFT JOIN category AS t4 ON t4.parent_category = t3.category_id
WHERE t1.category_descr = 'Frames';

只要category_id <&lt; 100,即只有两位数,但可以轻松调整。是的,一个完全疯狂/尴尬/(你说出来的)解决方案,但它适用于我的目的。

答案 1 :(得分:0)

如果你的叶子是有序的(即任何孩子都比其父母大),还有另一种方式:

SELECT tree.id FROM tree
LEFT JOIN tree t1 ON (t1.parent_id = tree.id)
WHERE t1.id IS NULL -- all leafs
AND tree.id > 3 -- greater than starting branch

答案 2 :(得分:0)

您可以尝试以下脚本:

Select * from
(SELECT id,name,parent_id FROM
    (SELECT id,name,parent_id,
           CASE WHEN id in (3) THEN @idlist := CONCAT(id)
                WHEN FIND_IN_SET(parent_id,@idlist) THEN @idlist := CONCAT(@idlist,',',id)
                END as checkId
    FROM categories
    ORDER BY id ASC) as T
WHERE checkId IS NOT NULL) N1
left join 
(SELECT id,name,parent_id FROM
    (SELECT id,name,parent_id,
           CASE WHEN id in (3) THEN @idlist := CONCAT(id)
                WHEN FIND_IN_SET(parent_id,@idlist) THEN @idlist := CONCAT(@idlist,',',id)
                END as checkId
    FROM categories
    ORDER BY id ASC) as T
WHERE checkId IS NOT NULL) N2 on N1.Id = N2.Parent_Id

sqlfiddle