如何使用 ID 作为外键来显示名称?

发布于 2024-09-07 20:51:05 字数 198 浏览 5 评论 0原文

这就是我所拥有的:

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

alt text

如何显示包含 ID、姓名和姓名(使用自引用 ID 的家长)的表格?

Here's what I have:

I have a table called Area that is self referential. If the column ParentAreaID is null it means it's a parent area. If it has a value, it means that it has a parents.

alt text

How can I show a table with ID, Name and NAME (of the parents using the self referential ID)?

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

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

发布评论

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

评论(1

冷…雨湿花 2024-09-14 20:51:05

您想要使用连接方式: 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;

You want to use 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文