简体   繁体   English

SQL查询从表中提取嵌套

[英]SQL Query to extract nesting from a table

I have a table in which references are made from one row to another. 我有一张表,其中引用是从一行到另一行的。 So basically something like this: 所以基本上是这样的:

ID | Name | ParentId
1  | Root | 
2  | Top  |  
3  | Sub1 | 1
4  | Sub2 | 2

The third column [ParentId] denotes the parent of the current row. 第三列[ParentId]表示当前行的父级。 My question is how do I get this nesting from the table using an SQL query. 我的问题是如何使用SQL查询从表中获取此嵌套。 The result I would like to see from the query is 我想从查询中看到的结果是

ID | Name | ParentName
1  | Root | NULL
2  | Top  | NULL
3  | Sub1 | Root
4  | Sub2 | Top

It might seem trivial as it is just replacing the id of the third column by the name of the item but I can't seem to figure it out. 似乎微不足道,因为它只是用项目名称替换了第三列的ID,但我似乎无法弄清楚。 Thanks for you help! 感谢您的帮助!

for a single level you could use a left join on the same table 对于单个级别,您可以在同一表上使用左联接

select a.id, a.name, b.name 
from my_table a  
left join my_table b on b.parentId = a.id 
select t1.id, t1.name, t2.name as parentName
from  table t1
left join  table t2
on t2.id = t1.parentId

The following query will help you to achieve your aim: 以下查询将帮助您实现目标:

SELECT
    t1.Id,
    t1.Name,
    (SELECT t2.Name FROM MyTable t2 WHERE t2.Id = t1.ParentId) ParentName 
FROM
    MyTable t1

If there is a lot of records in the table, by creating an index on the ParentId column you can run the above query faster than the JOIN . 如果表中有很多记录,则通过在ParentId列上创建索引,可以比JOIN更快地运行上述查询。

SQL Fiddle: hierarchyid example fiddles SQL小提琴: hierarchyid示例小提琴

I decided to take on a slightly different approach to answering this question, using the hierarchyid datatype that is supported natively inside of SQL server. 我决定采用一种稍微不同的方法来回答这个问题,使用SQL Server本身支持的hierarchyid数据类型。 hierarchyid was created to specifically handle record selection/query capabilities for nested relationships, and once you get used to some of the minor annoyances that come with the datatype (such as inline queries/subqueries which are often needed to arrive at the actual id value for a record), its actually pretty slick. hierarchyid创建专门负责处理记录选择/嵌套关系的查询功能,一旦你习惯了一些(附带的数据类型,如嵌入式查询/这往往需要以价值为实际的ID到达子查询未成年人的烦恼记录),它实际上非常漂亮。

I created a SQL fiddle showing step by step how to use the data-type for an example nested relationship of: 我创建了一个SQL小提琴,逐步显示了如何将数据类型用于以下示例的嵌套关系:

/Category One
    /Subcategory 1
        /Subcategory 1.1
        /Subcategory 1.2
        /Subcategory 1.3
    /Subcategory 2
    /Subcategory 3
        /Subcategory 3.1
            /Subcategory 3.1.1
    /Subcategory 4
/Category Two
    /Subcategory 1

NB: the tsql is rewritten and annotated MSDN example code, which was actually quite poorly made imo. 注意:tsql已被重写并带有注释的MSDN示例代码,而实际上这在imo中是很差的。 I have rewritten and presented it in such a way that I found it much easier to wrap my head around. 我以某种方式重写并介绍了它,使我发现将头缠绕起来要容易得多。

All proposed solution work fine for the posted question. 所有建议的解决方案都可以很好地解决发布的问题。 In my "real" situation the table is far more complicated and has already many APPLY statements (specific SQL Server, I guess) so I ended up using the "Outer Apply" variant of posted Join statement: With a AS ( SELECT id, name from Nesting )
SELECT id, name, ParentName from a OUTER APPLY ( SELECT name as ParentName from Nesting n where n.parentid = a.id
) AS FOO
在我的“实际”情况下,该表要复杂得多,并且已经有许多APPLY语句(我想是特定的SQL Server),所以我最终使用了发布的Join语句的“外部应用”变体: With a AS ( SELECT id, name from Nesting )
SELECT id, name, ParentName from a OUTER APPLY ( SELECT name as ParentName from Nesting n where n.parentid = a.id
) AS FOO
With a AS ( SELECT id, name from Nesting )
SELECT id, name, ParentName from a OUTER APPLY ( SELECT name as ParentName from Nesting n where n.parentid = a.id
) AS FOO

Thank you all so much! 非常感谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM