MYSQL根据节点向上和向下查询所有节点
Posted 冰封的心
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL根据节点向上和向下查询所有节点相关的知识,希望对你有一定的参考价值。
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根据节点向上和向下查询所有节点的主要内容,如果未能解决你的问题,请参考以下文章