SELECT
@r AS _id,
(SELECT @r := parent_id FROM sys_dept WHERE id = _id) AS parent_id,
@l := @l + 1 AS lvl FROM (SELECT @r := 子节点ID, @l := 0) vars,
sys_dept h WHERE @r <> 0 AND parent_id > 0
05-18
1116
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-04
2721
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-20
2720
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-18
1152
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-13