oracle - 通过先前的预言机连接

标签 oracle recursive-query connect-by

我有一个包含分层元素的表

表:

A       B 
P1      -
P2      P1
C1      P2
C2      P2

B是来自同一个表的外键

查询是:

SELECT level niveau, A
from table parent, table child
START WITH A IN
( 'P0','P1','C2')
CONNECT BY PRIOR  A= B

结果

1 P1
1 P2
  2 C1
  2 C2
1 C2

为什么会出现“2 C2”?

尽管不正确,但期望的结果是:

1 P1
1 P2
  2 C1
1 C2

最佳答案

首先评估

Connect by条件,然后评估start with。在您的情况下,C2 既是 P2 的子级,又是层次结构的根。这就是它在您的结果中出现两次的原因。

来自Oracle Documentaion

Oracle processes hierarchical queries as follows:

A join, if present, is evaluated first, whether the join is specified in the FROM clause or with WHERE clause predicates.

The CONNECT BY condition is evaluated.

Any remaining WHERE clause predicates are evaluated.

Oracle then uses the information from these evaluations to form the hierarchy using the following steps:

Oracle selects the root row(s) of the hierarchy--those rows that satisfy the START WITH condition.

Oracle selects the child rows of each root row. Each child row must satisfy the condition of the CONNECT BY condition with respect to one of the root rows.

Oracle selects successive generations of child rows. Oracle first selects the children of the rows returned in step 2, and then the children of those children, and so on. Oracle always selects children by evaluating the CONNECT BY condition with respect to a current parent row.

If the query contains a WHERE clause without a join, then Oracle eliminates all rows from the hierarchy that do not satisfy the condition of the WHERE clause. Oracle evaluates this condition for each row individually, rather than removing all the children of a row that does not satisfy the condition.

Oracle returns the rows in the order shown in Figure 9-1. In the diagram, children appear below their parents. For an explanation of hierarchical trees,

关于oracle - 通过先前的预言机连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12746094/

相关文章:

sql - oracle 9i 使用给定的 child 获得树的最高成员

oracle - 如何使用 Oracle CONNECT BY 获取链接到某个值的层次结构中的所有值

sql-server - Oracle 'CONNECT BY PRIOR' 和 'ORDER SIBLINGS BY' 的 SQL Server 等效项

Oracle Linux 7 (AWS) 上的 Oracle 12c 到 Hadoop 节点

Azure 数据工厂 - 将 FTP 递归复制到数据库

sql - Oracle:将国家/地区分隔为 N 个边界

sql-server - sql server如何检索树中从任何 child 到祖先的所有节点?

javascript - 如何在 JavaScript 中正确引用 Oracle SQL 字符串

oracle - DBMS_LOB.SUBSTR() 抛出 "character string buffer too small"错误

c# - 首先在 EF 4.1 代码中以 "schema.tablename"格式映射 POCO,使用 dotconnect for Oracle