返回自连接表上的父/子关系
我需要能够使用 SQL 返回所有级别的给定父 ID 的所有子列表。
该表看起来像这样:
ID ParentId Name
---------------------------------------
1 null Root
2 1 Child of Root
3 2 Child of Child of Root
给出一个 ID '1',我将如何返回整个列表......?嵌套深度也没有限制...
谢谢,
基隆
I need to be able to return a list of all children given a parent Id at all levels using SQL.
The table looks something like this:
ID ParentId Name
---------------------------------------
1 null Root
2 1 Child of Root
3 2 Child of Child of Root
Give an Id of '1', how would I return the entire list...? There is no limitation on the depth of the nesting either...
Thanks,
Kieron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取以这种方式存储的给定
@ParentId
的所有子项,您可以使用递归 CTE。To get all children for a given
@ParentId
stored in that manner you could use a recursive CTE.