SQL迭代查询 PL/SQL

with ORG_Tree(ObjectId,parentID) 
as
(
    select a.ObjectId,a.parentID from Ot_Organizationunit a where Name in ('江苏区域','浙江区域','苏州区域')
    Union ALL
    select b.ObjectId,b.parentID from Ot_Organizationunit b inner join ORG_Tree T on b.parentID=T.ObjectID
)
select ouser.Name,ouser.code from OT_User ouser inner join ORG_Tree ot on ouser.parentid=ot.ObjectID

 

-- Get childs by parent id
WITH Tree
AS
(
    SELECT Id,ParentId FROM dbo.Node P WHERE P.Id = 21 -- parent id
    UNION ALL
    SELECT C.Id,C.ParentId FROM dbo.Node C
    INNER JOIN Tree T ON C.ParentId = T.Id
)
SELECT * FROM Tree

-- Get parents by child id
WITH Tree
AS
(
    SELECT Id,ParentId FROM dbo.Node C WHERE C.Id = 57 -- child id
    UNION ALL
    SELECT P.Id,P.ParentId FROM dbo.Node P
    INNER JOIN Tree T ON P.Id = T.ParentId
)
SELECT * FROM Tree

 

参:SQL Server 父子迭代查询语句,树状查询

相关文章:

  • 2021-06-05
  • 2022-01-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-28
  • 2021-10-25
相关资源
相似解决方案