递归查询树tree 根据子节点查询父级

根据父节点查询子集 https://blog.csdn.net/wjy550823795/article/details/100703615

表结构就是正常的id 与 父类id 如上链接所示

SELECT T1.LEVEL1,T2.*
FROM ( 
    SELECT 
        @r AS _id, 
        (SELECT @r := parent_id FROM project_grouping WHERE gid = _id) AS parent_id, 
        @l := @l + 1 AS LEVEL1 
    FROM 
        (SELECT @r := 8, @l := 0) vars, 
        project_grouping h 
    WHERE @r <> 0) T1 
JOIN project_grouping T2 
ON T1._id = T2.gid 
ORDER BY T1.LEVEL1 DESC 

@r:= #{8} 为你要查询的子节点id

粘贴亲测可用

返回结果

递归查询树tree 根据子节点查询父级_第1张图片

你可能感兴趣的:(java,mysql,递归查询,引用,tree树)