这就是我所拥有的:
我有一个名为Area的表,它是自引用的。如果列ParentAreaID为null,则表示它是父区域。如果它有值,则表示它有父母。
如何显示包含ID,姓名和名称的表(使用自引用ID的父母)?
答案 0 :(得分:0)
您想使用connect by: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;