SQL:递归路径 [英] SQL: Recursive Path
本文介绍了SQL:递归路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在 SQL 中创建树解析器"?
Is it possible to create a "tree resolver" in SQL?
我有一张桌子:
ID Name Parent
1 a
2 b 1
3 c 1
4 d 3
现在我想要一个返回的 SQL 查询:
Now I want a SQL query that returns:
ID PATH
1 /a
2 /a/b
3 /a/c
4 /a/c/d
这可以用 SQL 实现吗?对我来说,这会让很多事情变得更容易.任何帮助将不胜感激!
Is this possible with SQL? It would make many things easier for me. Any help would really be appreciated!
推荐答案
根据使用的数据库服务器,此功能可能已经为您提供.否则,您可以创建一个调用自身以返回此信息的函数,或实现一个 物化路径解决方案.
Depending on what database server use, this functionality may be provided for you already. Otherwise you can create a function that call itself to return this information, or implement a Materialized Path solution.
更新:
对于 DB2,您可以使用 递归公用表表达式.
For DB2 you can make use of Recursive Common Table Expressions.
这篇关于SQL:递归路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文