SQL 从自引用中按父子顺序检索父子关系 table
SQL to retrieve parent-child relationship in parent-child order, from a self referencing table
我使用以下查询从自引用父项的 table 中检索父子关系数据。
-- go down the hierarchy and get the childs
WITH ChildLocations(LocationId, FkParentLocationId, [Level])
AS
(
(
-- Start CTE off by selecting the home location of the user
SELECT l.LocationId, l.FkParentLocationId, 0 as [Level]
FROM Location l
WHERE l.LocationId = @locationId
)
UNION ALL
-- Recursively add locations that are children of records already found in previous iterations.
SELECT l2.LocationId, l2.FkParentLocationId, [Level] + 1
FROM ChildLocations tmp
INNER JOIN Location l2
ON l2.FkParentLocationId = tmp.LocationId
)
INSERT INTO @tmp
SELECT * from ChildLocations;
table有以下字段:
LocationId、FkParentLocationId、FkLocationTypeId 等...
这工作正常,但我想检索它的方式如下:
Parent 1
Child 1
Child 2
Child 21
Child 3
Child 31
Parent 2
Child 4
Child 5
Child 6
目前给出的是这样的:
Parent 1
Parent 2
Child 1
Child 2
Child 3
Child 4
etc....
我如何修改上面的内容以按我想要的顺序获取它。
如何附加一个 'order' 字段?这可能是一种方法:
WITH ChildLocations(LocationId, FkParentLocationId, [Level])
AS
(
(
-- Start CTE off by selecting the home location of the user
SELECT l.LocationId, l.FkParentLocationId, 0 as [Level],
cast( str( l.locationId ) as varchar(max) ) as orderField
FROM Location l
WHERE l.LocationId = @locationId
)
UNION ALL
-- Recursively add locations that are children ...
SELECT l2.LocationId, l2.FkParentLocationId, [Level] + 1,
tmp.orderField + '-' +
str(tmp.locationId) as orderField
FROM ChildLocations tmp
INNER JOIN Location l2
ON l2.FkParentLocationId = tmp.LocationId
)
SELECT * from ChildLocations order by orderField;
请记住,Order by
在 Insert
中是不允许的。
我使用以下查询从自引用父项的 table 中检索父子关系数据。
-- go down the hierarchy and get the childs
WITH ChildLocations(LocationId, FkParentLocationId, [Level])
AS
(
(
-- Start CTE off by selecting the home location of the user
SELECT l.LocationId, l.FkParentLocationId, 0 as [Level]
FROM Location l
WHERE l.LocationId = @locationId
)
UNION ALL
-- Recursively add locations that are children of records already found in previous iterations.
SELECT l2.LocationId, l2.FkParentLocationId, [Level] + 1
FROM ChildLocations tmp
INNER JOIN Location l2
ON l2.FkParentLocationId = tmp.LocationId
)
INSERT INTO @tmp
SELECT * from ChildLocations;
table有以下字段: LocationId、FkParentLocationId、FkLocationTypeId 等...
这工作正常,但我想检索它的方式如下:
Parent 1
Child 1
Child 2
Child 21
Child 3
Child 31
Parent 2
Child 4
Child 5
Child 6
目前给出的是这样的:
Parent 1
Parent 2
Child 1
Child 2
Child 3
Child 4
etc....
我如何修改上面的内容以按我想要的顺序获取它。
如何附加一个 'order' 字段?这可能是一种方法:
WITH ChildLocations(LocationId, FkParentLocationId, [Level])
AS
(
(
-- Start CTE off by selecting the home location of the user
SELECT l.LocationId, l.FkParentLocationId, 0 as [Level],
cast( str( l.locationId ) as varchar(max) ) as orderField
FROM Location l
WHERE l.LocationId = @locationId
)
UNION ALL
-- Recursively add locations that are children ...
SELECT l2.LocationId, l2.FkParentLocationId, [Level] + 1,
tmp.orderField + '-' +
str(tmp.locationId) as orderField
FROM ChildLocations tmp
INNER JOIN Location l2
ON l2.FkParentLocationId = tmp.LocationId
)
SELECT * from ChildLocations order by orderField;
请记住,Order by
在 Insert
中是不允许的。