表格中的笛卡尔产品内部关系。 SQL
我有一个由两列构建的表:人(文本)和主管(文本) - 一种层次结构表。
表名:等级制度
的 | 主管 |
---|---|
·玛丽( Bob | Alice |
鲍勃·爱丽丝·玛丽· | 苏珊· |
Mary Susan Alice | 爱丽丝·戴维 |
David | Mary) |
我想创建一个包含的第三列 主管的主管 - “ sup_supervisor” 下面的示例:
人员 | 主管 | SUP_SUPERVISOR |
---|---|---|
BOB | ALICE | DAVID |
MAVEN | ALICE是 | |
ALICE | ALICE | MADER MADY |
MADY | MADAR | SUSAN |
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
person | supervisor |
---|---|
Bob | Alice |
Mary | Susan |
Alice | David |
David | Mary |
I wish to create a third column that contains
the supervisors of the supervisors- "sup_supervisor"
example below:
person | supervisor | sup_supervisor |
---|---|---|
Bob | Alice | David |
Mary | Susan | |
Alice | David | Mary |
David | Mary | Susan |
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那不是如何设计关系表。首先,您应该拥有独特的主键,只使用该人的名字就意味着您不能处理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