2

我需要一个 SQL 查询给我一个项目的完整树路径。表格看起来像这样,MyItem_MyItemId 和 MyItemMapping_MyItemId 之间存在 1:n 关系。

表我的项目:

MyItem_MyItemId | MyItem_Title 
1 | Desktop
2 | Workspace
3 | Folder1
4 | Folder2
5 | Folder3
6 | Folder4
...

表 MyItemMapping:

MyItemMapping_MyItemId | MyItemMapping_MyItemParentId
4 | 3
3 | 2
2 | 1
1 | NULL
5 | 2
6 | 2
...

现在我需要一个查询,它带来了 Folder2 的路径,例如“Desktop\Workspace\Folder1\Folder2.

我用递归查询(见下文)尝试了它,但 SQL Server 需要大约 10 秒来解决它。我在数据库中只有 5000 条记录。我可以弄清楚,通过这个查询,路径是为所有 5000 条记录计算的,但我只需要一项。有谁能够帮助我?

WITH 
MyTable as
(
 select MyItem_MyItemId, MyItem_Title, MyItemMapping_MyItemParentId 
 from MyItem inner join MyItemMapping on MyItem_MyItemId = MyItemMapping_MyItemId
),
RecursiveTable AS 
(
  select t.MyItem_MyItemId, t.MyItem_Title, t.MyItemMapping_MyItemParentId
  from MyTable as t
  Where MyItemMapping_MyItemParentId is null 
  union all
  select  t.MyItem_MyItemId,  CAST(RecursiveTable.MyItem_Title + '\' + t.MyItem_Title AS NVARCHAR(max)), t.MyItemMapping_MyItemParentId
  from MyTable as t
  JOIN RecursiveTable ON RecursiveTable.MyItem_MyItemId = t.MyItemMapping_MyItemParentId
)   
select MyItem_Title from RecursiveTable where MyItem_MyItemid = 4

非常感谢你。

最好的祝福。马克

4

2 回答 2

1

朝相反的方向移动。选择必要的项目后,使用带有FOR XML子句的模式来创建反斜杠分隔的有效值列表。

DECLARE @MyItemMapping_MyItemId int = 4
;WITH cte AS
 (
  SELECT MyItemMapping_MyItemId, MyItemMapping_MyItemParentId, 1 AS rn
  FROM MyItemMapping
  Where MyItemMapping_MyItemId = @MyItemMapping_MyItemId 
  UNION ALL
  SELECT m.MyItemMapping_MyItemId, m.MyItemMapping_MyItemParentId, rn + 1
  FROM MyItemMapping m JOIN cte c ON c.MyItemMapping_MyItemParentId = m.MyItemMapping_MyItemId
  )
  SELECT STUFF((SELECT '/' + m.MyItem_Title                   
                FROM cte c JOIN MyItem m                 
                  ON c.MyItemMapping_MyItemId = m.MyItem_MyItemId
                ORDER BY c.rn DESC                    
                FOR XML PATH, TYPE).value('.[1]', 'nvarchar(max)'), 1, 1, '') AS pathFolder 

路径文件夹

结果:

PathFolder

Desktop/Workspace/Folder1/Folder2

SQLFiddle上的演示

于 2013-03-23T16:02:02.977 回答
0

请检查查询:

declare @MyItem as table (MyItem_MyItemId INT, MyItem_Title nvarchar(50))

insert into @MyItem values 
(1, 'Desktop'),
(2, 'Workspace'),
(3, 'Folder1'),
(4, 'Folder2')

declare @MyItemMapping as table (MyItemMapping_MyItemId INT, MyItemMapping_MyItemParentId int)

insert into @MyItemMapping values
(4, 3),
(3, 2),
(2, 1),
(1, NULL)

select * From @MyItem
select * From @MyItemMapping

;WITH parent AS
(
    SELECT MyItemMapping_MyItemId, MyItemMapping_MyItemParentId, CAST( b.MyItem_Title as nvarchar(max))MyItem_Title
    from @MyItemMapping a INNER JOIN @MyItem b on a.MyItemMapping_MyItemId=b.MyItem_MyItemId
    WHERE MyItemMapping_MyItemParentId is NULL

    UNION ALL 
    SELECT t.MyItemMapping_MyItemId, t.MyItemMapping_MyItemParentId ,CAST( parent.MyItem_Title+'/'+ b.MyItem_Title as nvarchar(max))MyItem_Title
    FROM parent INNER JOIN @MyItemMapping t ON parent.MyItemMapping_MyItemId =  t.MyItemMapping_MyItemParentId
        INNER JOIN @MyItem b on t.MyItemMapping_MyItemId=b.MyItem_MyItemId
)

SELECT MyItem_Title FROM  parent where MyItemMapping_MyItemId=(select MAX(MyItem_MyItemId) from @MyItem)

或者

declare @output varchar(max)
;WITH parent AS
(
    SELECT MyItemMapping_MyItemId, MyItemMapping_MyItemParentId
    from @MyItemMapping
    WHERE MyItemMapping_MyItemParentId is NULL

    UNION ALL 
    SELECT t.MyItemMapping_MyItemId, t.MyItemMapping_MyItemParentId 
    FROM parent INNER JOIN @MyItemMapping t ON parent.MyItemMapping_MyItemId =  t.MyItemMapping_MyItemParentId
)

SELECT @output = COALESCE(@output + '/', '')+ (select MyItem_Title from @MyItem where MyItem_MyItemId=MyItemMapping_MyItemId) 
FROM  parent where MyItemMapping_MyItemId <= 4 group by MyItemMapping_MyItemId order by MyItemMapping_MyItemId

select @output
于 2013-03-23T09:43:44.100 回答