SQL Server - 以多对多关系获取连续的所有子项?

时间:2011-09-02 14:54:50

标签: sql sql-server many-to-many hierarchical-data

我正在尝试在SQL Server中编写递归查询,该查询基本上列出了给定父级的父子层次结构。父母可以有多个孩子,孩子可以属于多个父母,因此它以多对多的关系存储。

我从另一个有点相关的问题中修改了以下查询,但这并不是一直到树,只选择第一级子...

DECLARE @ObjectId uniqueidentifier
SET @ObjectId = '1A213431-F83D-49E3-B5E2-42AA6EB419F1';

WITH Tree AS
(
   SELECT A.*
   FROM Objects_In_Objects A
   WHERE A.ParentObjectId = @ObjectId

   UNION ALL

   SELECT B.*
   FROM Tree A
   JOIN Objects_In_Objects B
   ON A.ParentObjectId = B.ObjectId
)
SELECT *
FROM Tree
INNER JOIN Objects ar on tree.ObjectId = ar.ObjectId

有没有人知道如何修改查询以一直向下“树”?或者使用上述结构是不可能的?

物件

列:ObjectId | Name

Objects_In_Objects

列:ObjectId | ParentObjectId

示例数据:

物件

ObjectId                             | Name
1A213431-F83D-49E3-B5E2-42AA6EB419F1 | Main container  
63BD908B-54B7-4D62-BE13-B888277B7365 | Sub container  
71526E15-F713-4F03-B707-3F5529D6B25E | Sub container 2  
ADA9A487-7256-46AD-8574-0CE9475315E4 | Object in multiple containers

对象中的对象

ObjectId                             | ParentObjectId                    
ADA9A487-7256-46AD-8574-0CE9475315E4 | 71526E15-F713-4F03-B707-3F5529D6B25E
ADA9A487-7256-46AD-8574-0CE9475315E4 | 63BD908B-54B7-4D62-BE13-B888277B7365
63BD908B-54B7-4D62-BE13-B888277B7365 | 1A213431-F83D-49E3-B5E2-42AA6EB419F1
71526E15-F713-4F03-B707-3F5529D6B25E | 1A213431-F83D-49E3-B5E2-42AA6EB419F1

3 个答案:

答案 0 :(得分:1)

这样的递归CTE(公共表格表达式) goo 一路

试试这个:

;WITH Tree AS
(
   SELECT A.ObjectID, A.ObjectName, o.ParentObjectID, 1 AS 'Level'
   FROM dbo.Objects A
   INNER JOIN dbo.Objects_In_Objects o ON A.ObjectID = o.ParentObjectID
   WHERE A.ObjectId = @ObjectId           -- use the A.ObjectId here

   UNION ALL

   SELECT A2.ObjectID, A2.ObjectName, B.ParentObjectID, t.Level + 1 AS 'Level'
   FROM Tree t 
   INNER JOIN dbo.Objects_In_Objects B ON B.ParentObjectID = t.ObjectID
   INNER JOIN dbo.Objects A2 ON A2.ObjectId = B.ObjectId        
)
SELECT *
FROM Tree
INNER JOIN dbo.Objects ar on tree.ObjectId = ar.ObjectId

如果你改变这个 - 现在这对你有用吗? (我添加了Level列 - 通常有助于理解每行的层次结构中的“深度”)

我似乎在我的SQL Server实例上得到了正确的输出,至少......

答案 1 :(得分:1)

declare @Objects_In_Objects table
(
  ObjectID uniqueidentifier, 
  ParentObjectId uniqueidentifier
)

declare @Objects table
(
  ObjectId uniqueidentifier, 
  Name varchar(50)
)

insert into @Objects values
('1A213431-F83D-49E3-B5E2-42AA6EB419F1', 'Main container'),  
('63BD908B-54B7-4D62-BE13-B888277B7365', 'Sub container'),  
('71526E15-F713-4F03-B707-3F5529D6B25E', 'Sub container 2'),  
('ADA9A487-7256-46AD-8574-0CE9475315E4', 'Object in multiple containers')

insert into @Objects_In_Objects values
('ADA9A487-7256-46AD-8574-0CE9475315E4', '71526E15-F713-4F03-B707-3F5529D6B25E'),
('ADA9A487-7256-46AD-8574-0CE9475315E4', '63BD908B-54B7-4D62-BE13-B888277B7365'),
('63BD908B-54B7-4D62-BE13-B888277B7365', '1A213431-F83D-49E3-B5E2-42AA6EB419F1'),
('71526E15-F713-4F03-B707-3F5529D6B25E', '1A213431-F83D-49E3-B5E2-42AA6EB419F1')


DECLARE @ObjectId uniqueidentifier
SET @ObjectId = '1A213431-F83D-49E3-B5E2-42AA6EB419F1';

WITH Tree AS
(
   SELECT A.ObjectID,
          A.ParentObjectId
   FROM @Objects_In_Objects A
   WHERE A.ParentObjectId = @ObjectId

   UNION ALL

   SELECT B.ObjectID,
          B.ParentObjectId
   FROM Tree A
   JOIN @Objects_In_Objects B
   ON B.ParentObjectId = A.ObjectId
)
SELECT *
FROM Tree
INNER JOIN @Objects ar on tree.ObjectId = ar.ObjectId;

这是你在找什么? http://data.stackexchange.com/stackoverflow/q/111357/

答案 2 :(得分:0)