SQL从自引用表中检索父子顺序中的父子关系

时间:2023-01-19 00:17:23

I use the following query to retrieve the parent-child relationship data, from a table which is self referencing to the parent.

我使用以下查询从自引用父表的表中检索父子关系数据。

    -- 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;

The table has the following fields: LocationId, FkParentLocationId, FkLocationTypeId, etc...

该表包含以下字段:LocationId,FkParentLocationId,FkLocationTypeId等...

This works fine, but how I want to retrieve it is as follows:

这工作正常,但我想如何检索它如下:

Parent 1
    Child 1
    Child 2
      Child 21
    Child 3
      Child 31
Parent 2
    Child 4
    Child 5
    Child 6

What is currently gives is like:

目前给出的是:

Parent 1
Parent 2
    Child 1
    Child 2
    Child 3
    Child 4
 etc....

How can I modify the above to get it in the order I want.

如何修改上面的内容以按我想要的顺序获取它。

1 个解决方案

#1


1  

What about to append an 'order' field? This may be an approach:

如何附加“订单”字段?这可能是一种方法:

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;

Remember than Order by in an Insert is not allowed.

请记住,不允许在插入中使用Order by。

Take a look a sample

看看样品

#1


1  

What about to append an 'order' field? This may be an approach:

如何附加“订单”字段?这可能是一种方法:

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;

Remember than Order by in an Insert is not allowed.

请记住,不允许在插入中使用Order by。

Take a look a sample

看看样品