Oracle SQL 分层查询
Oracle SQL Hierarchical Query
我正在尝试构建一个 Oracle sql 查询,因此 select 只有 table 中最新的 child 记录,该记录具有记录的层次结构。
table 中的某些记录引用了 parent 记录(parent_id 列),这是对其 ID 的引用。这些 child 条记录也可能有 children。
我想构建一个查询,其中只获取最新的 child(如果它有 one/any)。
例如
my_table
id parent_id name
111 null 'no parent'
222 null 'one child'
333 222
444 null 'two children'
555 444
666 555
因此,所需的结果集将是
select id from <query>
111
333
666
谢谢
这可以通过在同一个 table 上使用子查询来实现。
select a.id
from my_table a
where not exists
(select 1
from my_table b
where b.parent_id = a.id)
我正在尝试构建一个 Oracle sql 查询,因此 select 只有 table 中最新的 child 记录,该记录具有记录的层次结构。
table 中的某些记录引用了 parent 记录(parent_id 列),这是对其 ID 的引用。这些 child 条记录也可能有 children。
我想构建一个查询,其中只获取最新的 child(如果它有 one/any)。 例如
my_table
id parent_id name
111 null 'no parent'
222 null 'one child'
333 222
444 null 'two children'
555 444
666 555
因此,所需的结果集将是
select id from <query>
111
333
666
谢谢
这可以通过在同一个 table 上使用子查询来实现。
select a.id
from my_table a
where not exists
(select 1
from my_table b
where b.parent_id = a.id)