sql递归显示层级数据

Posted 荆棘中的百合花

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql递归显示层级数据相关的知识,希望对你有一定的参考价值。

;with catChild as(select *,cast(right(00000+cast(Sort as varchar),5) as varchar(max)) as ssort 
from Category where ID = ‘123
    union all select t.*,
    cast(ssort+right(00000+cast(t.Sort as varchar),5) as varchar(max))
     from catChild, Category  t
    where catChild.ID = t.ParentID)
select * from catChild where IsVolumes=1  order by ssort,Sort


;with cte as
 (select ID,Name,ParentID,1 level,
         cast(right(00000+cast(Sort as varchar),5) as varchar(max)) as ssort
    from Category where ID = 111A 
  union all select t.ID,t.Name,t.ParentID,c.level + 1,
       cast(ssort+right(00000+cast(t.Sort as varchar),5) as varchar(max))
    from Category t
    join cte c on t.ParentID = c.ID)
select ID, Name, ParentID, level,ssort
  from cte order by ssort

 

以上是关于sql递归显示层级数据的主要内容,如果未能解决你的问题,请参考以下文章

sqlserver递归使用,简单但是比较实用

Php无限层级并显示层级数

Php无限层级并显示层级数

php递归

SQL 递归查询,意淫CTE递归的执行步骤

thinkphp5.0递归获取栏目层级