【发布时间】: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_Id和Child_Id的定义非常规且相当混乱。"Root node is Jewelry Child_Id=0" -
所以“parent_id”实际上是指“id”,而“child_id”实际上是指“parent_id”。整洁的。另外,你需要一个前序遍历。
标签: sql sql-server sql-server-2005 sql-server-2012