【问题标题】:How To Arrange Table Data in Parent_Id And Child_Id Wise Via Sql-Server Query如何通过 Sql-Server 查询在 Parent_Id 和 Child_Id 中明智地排列表数据
【发布时间】:2018-12-13 12:26:51
【问题描述】:

如何通过 Sql-Server 查询在 Parent_Id 和 Child_Id 中明智地排列表数据?

我的查询

 select CLevel_Id,Category_Name,Parent_Id,Child_Id,Level_Id from Category_Level
 order by Parent_Id asc

电流输出

CLevel_Id   Category_Name   Parent_Id   Child_Id    Level_Id
12             Jewelry           1           0             1
14             Rings             2           1             2
15             Men-Rings         3           2             3
17             Women-Rings       4           2             3
18             Earrings          5           1             2
20             Women-Earings     6           5             3
1013           Metal-Fashion     7           3             4
1015           Diamond-Fashion   8           4             4
1016           Semi-Set          9           6             4

预期输出

CLevel_Id   Category_Name   Parent_Id   Child_Id    Level_Id
12             Jewelry           1           0             1
14             Rings             2           1             2
15             Men-Rings         3           2             3
1013           Metal-Fashion     7           3             4
17             Women-Rings       4           2             3
1015           Diamond-Fashion   8           4             4
18             Earrings          5           1             2
20             Women-Earings     6           5             3
1016           Semi-Set          9           6             4

请帮帮我

【问题讨论】:

  • 结果与您的查询不一致
  • 这里的根节点是什么?我希望看到一些记录的父母是NULL,但我没有看到这一点。您想要的答案可能只是递归分层查询。
  • 您想要 SQL Server 和 MySQL 的解决方案?
  • 您对Parent_IdChild_Id 的定义非常规且相当混乱。 "Root node is Jewelry Child_Id=0"
  • 所以“parent_id”实际上是指“id”,而“child_id”实际上是指“parent_id”。整洁的。另外,你需要一个前序遍历。

标签: sql sql-server sql-server-2005 sql-server-2012


【解决方案1】:

使用递归 CTE。

假设您每个级别最多有 9 个。使用单个数字作为 seq 级别。如果超过 9 个,则需要使用 2 位数字,例如 01、02 等

; with
rcte as
(
    -- Anchor member, seq = 1
    select  *, seq = convert(varchar(100), '1')
    from    Category_Level
    where   Child_Id    = 0

    union all

    -- recursive member, concatenate to the seq
    select  c.*, seq = convert(varchar(100), 
                    r.seq 
                +   convert(varchar(10), row_number() over (partition by r.seq 
                                                                order by c.Child_Id)))
    from    Category_Level c
        inner join rcte r   on  c.Child_Id  = r.Parent_Id
)
select  *
from    rcte 
order by seq

/* RESULT
CLevel_Id   Category_Name        Parent_Id   Child_Id    Level_Id    seq
----------- -------------------- ----------- ----------- ----------- -------
12          Jewelry              1           0           1           1
14          Rings                2           1           2           11
15          Men-Rings            3           2           3           111
1013        Metal-Fashion        7           3           4           1111
17          Women-Rings          4           2           3           112
1015        Diamond-Fashion      8           4           4           1121
18          Earrings             5           1           2           12
20          Women-Earings        6           5           3           121
1016        Semi-Set             9           6           4           1211

(9 rows affected)
*/

【讨论】:

  • 那么每次取parent的seq并追加child的order?太棒了!
  • 是的。所以你会得到一长串 seq 连接在一起
猜你喜欢
  • 2021-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
相关资源
最近更新 更多