Oracle中的简单递归查询 [英] Simple recursive query in Oracle
本文介绍了Oracle中的简单递归查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前在理解和编写递归查询时遇到一些麻烦.我知道递归查询用于搜索信息层次结构,但是我还没有找到一个可以遍历层次结构的简单在线解决方案.例如,假设我有一个对家谱建模的关系:
I'm currently having some trouble understanding and writing recursive queries. I understand that recursive queries are used to search through hierarchies of information, but I haven't found a simple solution online that can travel up a hierarchy. For example, let's say that I have a relation that models a family tree:
create table family_tree (
child varchar(10)
parent varchar(10)
);
如果我想编写一个遍历此家谱的递归查询,收集所有父母直到出身,我应该怎么做?
If I wanted to write a recursive query that travelled up this family tree, collecting all parents until origin, how should I go about this?
谢谢.
推荐答案
您可以使用connect by
子句.
在您的情况下,SQL可能类似于:
In your case, SQL might look like:
select child, parent, level
from family_tree
connect by prior parent = child
这篇关于Oracle中的简单递归查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文