时间:2021-07-01 10:21:17 帮助过:6人阅读
WITH cte
AS
(
SELECT * ,UnitID AS level FROM UnitTable WHERE UnitID=2
UNION ALL
SELECT g.*,level+1 FROM UnitTable g INNER JOIN cte
ON g.FatherID=cte.UnitID
)
SELECT * FROM cte
WITH cte
AS
(
SELECT *,0 as level FROM UnitTable WHERE FatherID=0
UNION ALL
SELECT g.*,level+1 FROM UnitTable g INNER JOIN cte
ON g.FatherID=cte.UnitID
)
SELECT * FROM cte
WITH cte
AS
(
SELECT * ,UnitID AS level FROM UnitTable WHERE UnitID=2
UNION ALL
SELECT g.*,g.UnitID AS level FROM UnitTable g INNER JOIN cte ON g.UnitId=cte.FatherID
)
SELECT * FROM cte
MYSQL根据节点向上和向下查询所有节点
标签:nbsp select mysql mysq rom 根据 unit level 节点