原表数据为:

SQL 无限级分类语句

 

此处用到了with关键字,在程序中也可以用递归实现,但觉得还是没有一条sql方便

with tb (ID,Name,ParentID,Sort)
as(
 select a.*,convert(varchar,convert(varchar,partid)+'->'+convert(varchar,ID))
 from test a where partid=0  --查询所有父节点
 UNION ALL
 select a.*,convert(varchar,sort+'->'+convert(varchar,a.ID))
 from test a inner join tb b on a.partid=b.ID
)
select * from tb order by sort
View Code

相关文章:

  • 2021-07-15
  • 2021-09-11
  • 2021-09-29
  • 2022-12-23
  • 2021-06-02
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-09-20
  • 2021-10-21
  • 2021-12-27
  • 2021-07-20
  • 2021-09-27
相关资源
相似解决方案