--递归小示例(适用于当你想查询一个表中有父级的时候 每一个类别的父级是谁)
with digui as
(
SELECT * FROM (
select *,1 ZIndex from 表名
WHERE ParentID IS NULL
) B
union all
select ss.*,c.ZIndex+1
from digui c
inner join 表名 ss on c.ID = ss.ParentID
)
select ID,Name,ParentID,ZIndex from digui order by ZIndex asc