需要在 Oracle 分层查询中显示父级下子级下的孙级

发布于 2024-11-03 11:17:53 字数 769 浏览 4 评论 0原文

我有一张表,其中有州、地区、地区、建筑物和班级的树。每行都有一个节点 ID 和一个父 ID。我正在使用

select name, child node, parent id from tableA  
connect by prior child node = parent id  

我得到的以下内容:

CA  
  CENTRAL REGION  
    FRESNO DISTRICT  
     ST Jim BUILDING  
     ST joe BUILDING  
     st tom BUILDING  
     st sue BUILDING  
       JIMS CLASS  
       JOES CLASS  
       TOM CLASS  
       SUE CLASS  

问题是在建筑级别,所有建筑物都被一个接一个地列出,然后所有的类一个接一个地列出。但是,我想列出建筑物及其作为下一个建筑物的父级的类,如下所示,

 FRESNO DISTRICT  
    st jim building  
      jims class  
    st joes building  
      joes class...  

节点 id 和父级 id 是正确的,只是列出了父级的所有子级,然后列出了该子级的所有子级。它没有显示哪个孩子与父母一起去。

我想从上到下显示树,例如 CA、中部地区、弗雷斯诺区、圣乔大楼、Ms Mary 班、Ms

I have a table that has a tree of state, region, district, building and classes. Each row has a node id and a parent id. I am using the following

select name, child node, parent id from tableA  
connect by prior child node = parent id  

i get:

CA  
  CENTRAL REGION  
    FRESNO DISTRICT  
     ST Jim BUILDING  
     ST joe BUILDING  
     st tom BUILDING  
     st sue BUILDING  
       JIMS CLASS  
       JOES CLASS  
       TOM CLASS  
       SUE CLASS  

Problem is that at building level all buildings are listed one after another and then all the classes one after another. However I want to list the building and the class it is a parent of then the next building as follows

 FRESNO DISTRICT  
    st jim building  
      jims class  
    st joes building  
      joes class...  

the node id and parent id are correct it just that all children are listed for a parent then all children for that child. it is not showing which child goes with the parent.

I want to show the tree from the top down such as CA, Central region, Fresno district, St Joe Building, Ms Mary class, Ms

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

临风闻羌笛 2024-11-10 11:17:53

应保留分层顺序,除非您使用不带 SIBLINGS 关键字的 ORDER BY。

select name, id, parent_id
from tableA
connect by prior id = parent_id
start with parent_id is null
order siblings by name;

使用此数据:

create table tableA(id number, name varchar2(100), parent_id number);

insert into tableA
select 1 id, 'CA' name, null parent_id from dual union all
select 2, 'CENTRAL REGION', 1 from dual union all
select 3, 'FRESNO DISTRICT', 2 from dual union all
select 4, 'ST Jim BUILDING', 3 from dual union all
select 5, 'ST joe BUILDING', 3 from dual union all
select 6, 'st tom BUILDING', 3 from dual union all
select 7, 'st sue BUILDING', 3 from dual union all
select 8, 'JIMS CLASS', 4 from dual union all
select 9, 'JOES CLASS', 5 from dual union all
select 10, 'TOM CLASS', 6 from dual union all
select 11, 'SUE CLASS', 7 from dual;

The hierarchical ordering should be preserved, unless you use an ORDER BY without the SIBLINGS keyword.

select name, id, parent_id
from tableA
connect by prior id = parent_id
start with parent_id is null
order siblings by name;

Using this data:

create table tableA(id number, name varchar2(100), parent_id number);

insert into tableA
select 1 id, 'CA' name, null parent_id from dual union all
select 2, 'CENTRAL REGION', 1 from dual union all
select 3, 'FRESNO DISTRICT', 2 from dual union all
select 4, 'ST Jim BUILDING', 3 from dual union all
select 5, 'ST joe BUILDING', 3 from dual union all
select 6, 'st tom BUILDING', 3 from dual union all
select 7, 'st sue BUILDING', 3 from dual union all
select 8, 'JIMS CLASS', 4 from dual union all
select 9, 'JOES CLASS', 5 from dual union all
select 10, 'TOM CLASS', 6 from dual union all
select 11, 'SUE CLASS', 7 from dual;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文