表格中的笛卡尔产品内部关系。 SQL

发布于 2025-02-03 19:02:42 字数 1143 浏览 4 评论 0原文

我有一个由两列构建的表:人(文本)和主管(文本) - 一种层次结构表。

表名:等级制度

主管
·玛丽( BobAlice
鲍勃·爱丽丝·玛丽·苏珊·
Mary Susan Alice爱丽丝·戴维
DavidMary)

我想创建一个包含的第三列 主管的主管 - “ sup_supervisor” 下面的示例:

人员主管SUP_SUPERVISOR
BOBALICEDAVID
MAVENALICE是
ALICEALICEMADER MADY
MADYMADARSUSAN

Bob的主管,David是爱丽丝的主管。因此 - 大卫是sup_supervisor。 请注意,玛丽没有SUP_SUPERVISOR,因为Susan没有主管。

这是特定结果查询的一个示例(不是我需要的,而是一般的想法): 从层次结构中选择主管 person =(从层次结构中选择person ='bob'的主管)。这将为我们提供鲍勃的sup_supervisor。

如何创建一个查询以创建此列(不是每次关系时)?

  • 奖励问题: 如果我想在创建第三列之后再次实现此功能,并添加了监督“ sup_supervisor”的人的第四列。这个概念会相似吗?

示例:bob->爱丽丝 - >大卫 - >玛丽

谢谢你

I have a table built of two columns: person (text) and supervisor (text)- a sort of hierarchy table.

table name: hierarchy

personsupervisor
BobAlice
MarySusan
AliceDavid
DavidMary

I wish to create a third column that contains
the supervisors of the supervisors- "sup_supervisor"
example below:

personsupervisorsup_supervisor
BobAliceDavid
MarySusan
AliceDavidMary
DavidMarySusan

Alice is the supervisor of Bob, and David is the supervisor of Alice. hence- David is the sup_supervisor.
notice that Mary does not have a sup_supervisor because Susan does not have a supervisor.

this is an example of a specific result- query (not what I need but has the general idea):
select supervisor from hierarchy
where person=(select supervisor from hierarchy where person = 'Bob'). this will give us Bob's sup_supervisor.

how do I create a query to generally create this column (not manually checking each time the relation)?

  • bonus question:
    If I wish to implement this once again- after creating the third column, adding a fourth built of those who supervise the "sup_supervisor". will the concept be similar?

example: for Bob -> Alice -> David -> Mary

Thank you

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

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

发布评论

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

评论(1

浅听莫相离 2025-02-10 19:02:42

那不是如何设计关系表。首先,您应该拥有独特的主键,只使用该人的名字就意味着您不能处理2个相同的人。

如果要引用另一个记录(在同一表或其他表中),则添加一个外键列,该列引用了所引用的表PK

that's not how to design relational tables. For a start, you should have primary key that is unique, just using the person's name means you can't handle 2 people with the same same.

If you want to reference another record (in the same or a different table) then you add a foreign key column that references the pk of the table being referenced

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