0

这是我所拥有的:

我有一个名为 Area 的表,它是自引用的。如果 ParentAreaID 列为 null,则表示它是父区域。如果它有值,则意味着它有一个父母。

替代文字

如何显示带有 ID、Name 和 NAME(使用自引用 ID 的父母)的表格?

4

1 回答 1

0

您想通过以下方式使用连接: http: //psoug.org/reference/connectby.html

create table sample (id number, parentid number, name varchar2(50));

insert into sample values(1,null,'aaa');
insert into sample values(2,NULL,'bbb');
insert into sample values(3,NULL,'ccc');
insert into sample values(4,null,'ddd');

insert into sample values(5,1,'aaa1');
insert into sample values(6,2,'bbb2');
insert into sample values(7,3,'ccc3');
insert into sample values(8,4,'ddd4');

insert into sample values(9,5,'aaa11');
insert into sample values(10,6,'bbb22');
insert into sample values(11,7,'ccc33');
insert into sample values(12,8,'ddd44');

SELECT ID,PARENTID, NAME, CONNECT_BY_ROOT NAME PARENTNAME,
 SYS_CONNECT_BY_PATH(name, '/')
  FROM SAMPLE
  start with parentid is null
 CONNECT BY PRIOR ID = PARENTID
 order siblings by id;
于 2010-06-30T17:08:47.733 回答