1、使用With As做数据库递归,调优树形表结构

例如:设计表结构简化如:ID、ParentID、Name;这里的ParentID就是这个表本身的某个ID

 

WITH cte AS
(
    SELECT a.* FROM table1 AS a WHERE ID=5
    UNION ALL
    SELECT b.* FROM table1 AS b INNER JOIN cte ON 
    b.ParentID=cte.ID
)

select * from cte  

 

;with cte as
(
select OrgId, OrgName, IsOne, ParentId,OrgName as rid from Org where IsOne=1
union all
select c.OrgId, c.OrgName, c.IsOne, c.ParentId,p.rid from cte p 
inner join Org c on p.OrgId=c.ParentId
)
显示父级名称

相关文章:

  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-10
  • 2021-09-22
猜你喜欢
  • 2021-10-03
  • 2021-08-22
  • 2021-11-20
  • 2022-01-19
  • 2021-09-04
相关资源
相似解决方案