需要一个 Oracle 分层查询,该查询仅返回子项与搜索字符串匹配的记录的完整树
这是此查询的完整示例数据集,没有对树进行任何修剪,其中没有节点与搜索字符串匹配:
Level parent id text --------------------------------------------- 0 0 1 toplevel 1 1 2 foo 1 1 3 sumthin else 1 1 4 foo 0 0 7 toplevel2 1 7 8 secondlevel 1 7 9 anothersecondlevel
如果用户搜索“foo”,我需要返回以下内容:
0 0 1 toplevel 1 1 2 foo 1 1 4 foo
实际情况有点复杂(即三个级别)在我想返回的树中)但这抓住了问题。在英语中,返回与从文本列上的匹配节点开始的搜索字符串相匹配的节点的祖先树,并返回所有祖先。
我是 Oracle 的新手(至少最近),并且尝试添加到 CONNECT BY 子句但没有任何成功 - 总是返回以下内容:
1 1 2 foo 1 1 4 foo
PS - Oracle 文档和示例暗示 CONNECT_BY_ROOT 将捕获祖先,但它似乎所做的一切是返回顶级(ROOT)值。
Here's the full example data set for this query without any pruning of trees where no node matches the search string:
Level parent id text --------------------------------------------- 0 0 1 toplevel 1 1 2 foo 1 1 3 sumthin else 1 1 4 foo 0 0 7 toplevel2 1 7 8 secondlevel 1 7 9 anothersecondlevel
I need to return the following if the user searches on 'foo':
0 0 1 toplevel 1 1 2 foo 1 1 4 foo
The real case is a bit more complex (i.e., three levels in the tree that I want to return) but this captures the issue. In English, return the ancestor tree for an node that matches the search string starting at the matching node on text column and return all ancestors.
I am new to Oracle (at least recently) and have tried adding to the CONNECT BY clause without any success - always returns the following:
1 1 2 foo 1 1 4 foo
PS - the oracle docs and examples on this imply that CONNECT_BY_ROOT will capture the ancestors but all it seems to do is return top level (ROOT) values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要从下往上遍历,重要的是
CONNECT BY PRIOR
之后的值的顺序)order by
用于反转输出(因为根是 foo),distinct
删除重复的顶级值:注意:如果您向 foo 添加一个子级并切换CONNCT BY PRIOR id = Parent
如果您想查看整个层次结构,您将获得子级,您可以找到树的顶部
(通过查找没有父级的行)
然后使用它作为 STARTWITH id(并反转外部查询中树遍历的顺序,id=parent):
To traverse from the bottom up the important bit is the order of values after the
CONNECT BY PRIOR
)The
order by
is used to reverse the output (as the root is foo) and thedistinct
removes the duplicate toplevel values:Note: if you add a child to foo and switch the CONNCT BY PRIOR id = parent you will get the children
if you want to see the whole hierarchy you could find the top, of the tree
(by looking for the row with no parent)
then use this as the START WITH id (and reverse the order of the tree traversal in the outer query, id = parent) :
取决于您对 LEVEL 列的使用(根据我的评论)。
有关 Oracle 分层查询的信息: http://download.oracle .com/docs/cd/B19306_01/server.102/b14200/queries003.htm
这个如果 LEVEL 是 Oracle 伪列,则返回您所要求的内容:
返回:
如果 LEVEL 是表中的列,则:
返回:
希望有帮助...
Depending upon your use of the LEVEL column (as per my comment).
Info on Oracle Hierarchical Queries: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries003.htm
This returns what you ask for if LEVEL is the Oracle pseudocolumn:
Returns:
If LEVEL is a column in your table then:
Returns:
Hope it helps...
我对“仅返回完整树的 Oracle 分层查询”部分的看法是:
要将树过滤到包含特定子级的树,您需要在最后一个 WHERE 中添加另一个条件以进一步过滤 INITIAL_Parent_ID。
查询将变为:
My take on "Oracle hierarchical query that returns only full trees" part would be:
To filter the trees to the ones containing the specific children you'd need to add another condition to the last WHERE to further filter INITIAL_Parent_ID.
The query would become: