先上个效果图吧

sql查询层级分类

CTE递归查询里面用了一些小的技巧,查询出结果以后在前端用表格展示出来,层级视觉效果还是很明显的

with tree as
(
select [ID],[Name],[Address],[Remark],SortNo,Cast(RANK() OVER(order by SortNo,Name) as nvarchar(4000)) Code,cast('' as varchar) as prex from SmartHospital where ID=1
union all
select a.[ID],a.Name,a.[Address],a.[Remark],a.SortNo,b.Code +Cast(RANK() OVER(order by a.SortNo,a.Name) as nvarchar(4000)),cast(b.prex+' ' as varchar) from SmartHospital a,tree b where a.PID=b.ID
)
select ID,prex+Name as Name,Address,Remark,SortNo from tree order by Code

 

相关文章:

  • 2021-06-23
  • 2022-12-23
  • 2021-06-24
  • 2022-12-23
  • 2021-07-22
  • 2021-11-04
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-30
相关资源
相似解决方案