【问题标题】:Find all parents of given ids in tree in SQL Server在 SQL Server 的树中查找给定 ID 的所有父级
【发布时间】:2017-05-31 18:31:45
【问题描述】:

我在单个表中有一个树结构,在另一个表中有项目关系。我需要从给定的项目中找出所有的父母。

SELECT Id FROM dbo.Items WHERE Selected = 1

由于我从该查询中得到 6,9,11,我需要返回 7、5、2 的父项 ID。

我认为我应该为此使用某种递归 CTE,但我不确定从哪里开始。

你能帮帮我吗?谢谢!

dbo.Relationship
Id       ParentId
3           6
6           7
8           7
7           2
4           9
9           5
5           2   


dbo.Items
Id              Selected
2                   0
3                   0
4                   0
5                   0
6                   1
7                   0
8                   0
9                   1
11                  1

【问题讨论】:

    标签: sql-server tree common-table-expression


    【解决方案1】:

    在项目和关系之间的相关 Id 上左连接。

    SELECT 
        Items.Id, 
        Relationship.ParentId 
    FROM Items
    LEFT JOIN Relationship ON Relationship.Id = Items.Id
    

    我过去曾使用它来获取所有父 ID:

    with compParent as
    (
        select * from Component Where ComponentId = @ComponentId
        union all
        select Component.* from Component join compParent on Component.ComponentId = 
        compParent.ContainerParentId
    )
    
    select * from compParent;
    

    我已经用它来获取所有孩子:

    with compChild as
    (
        select * from Component where ComponentId = @ParentId
        union all
        select Component.* from Component join compChild on Component.ContainerParentId = compChild.ComponentId
    )
    
    
    select * from compChild;
    

    您还可以查看已经发布的许多 stackOverFlow 帖子,以获得父母和/或孩子。或者简单的谷歌搜索“SQL Server Get Parents”

    【讨论】:

      【解决方案2】:

      您想获取所选项目的所有父项吗? 我改为使用临时表。

      if object_id('tempdb..#Relationship') is not null drop table #Relationship
      create table #Relationship(Id int,ParentId int)
      insert into #Relationship(Id,ParentId)
      SELECT 3,6 UNION
      SELECT 6,7 UNION
      SELECT 8,7 UNION
      SELECT 7,2 UNION
      SELECT 4,9 UNION
      SELECT 9,5 UNION
      SELECT 5,2
      if object_id('tempdb..#items') is not null drop table #items
      create table #items(Id int, Selected bit)
      insert into #items(Id,Selected)
      SELECT 2,0 UNION
      SELECT 3,0 UNION
      SELECT 4,0 UNION
      SELECT 5,0 UNION
      SELECT 6,1 UNION
      SELECT 7,0 UNION
      SELECT 8,0 UNION
      SELECT 9,1 UNION
      SELECT 11,1
      
      ;with cte AS (
         SELECT i.ID AS SelectedID,r.ParentId FROM #Items AS i INNER JOIN #Relationship AS r ON i.id=r.id WHERE i.Selected=1
         UNION ALL
         SELECT cte.SelectedID, r.ParentId FROM #Relationship AS r INNER JOIN CTE ON CTE.ParentId=r.id
      )
      SELECT * FROM cte ORDER BY cte.SelectedID
      

      它能给你一些帮助吗?

      SelectedID ParentId ------------ ------------ 6 7 6 2 9 5 9 2

      【讨论】:

      • 这正是我想要返回的,但你能告诉我如何用简单的 SELECT Id、Selected FROM Items 替换所有“SELECT {number}, {number} UNION”语句。也许这是一个愚蠢的问题。
      • @Pegaz 你可以忽略Relationship和Items with语句,它就像一个临时表,你只需在CTE子语句中用你的表名替换它们。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      相关资源
      最近更新 更多