簡體   English   中英

兩個外鍵引用另一個表的主鍵

[英]Two foreign keys reference the primary key of another table

所以我有兩張桌子

Person(personID, first_name, last_name);
Relation(relationID, child_personID, parent_personID);

personID 和relationID 都是主鍵。 child_personID 和 parent_personID 都是外鍵。

我想進行查詢,所以我有孩子和父母的名字和姓氏。

child.first_name child.last_name 和 parent.first_name, parent.last_name

解決此問題的一種方法是使用連接表別名 像這樣的東西:

select
    child.first_name,
    child.last_name,
    parent.first_name,
    parent.last_name
from relation r
    join person child on r.child_personID = child.id
    join person parent on r.parent_personID = parent.id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM