[英]Two foreign keys reference the primary key of another table
So I have two tables所以我有两张桌子
Person(personID, first_name, last_name);
Relation(relationID, child_personID, parent_personID);
personID and relationID are both primary keys. personID 和relationID 都是主键。 child_personID and parent_personID are both foreign keys.
child_personID 和 parent_personID 都是外键。
I want to make a query so I have the first names and last names of both the child and parent.我想进行查询,所以我有孩子和父母的名字和姓氏。
child.first_name child.last_name and parent.first_name, parent.last_name child.first_name child.last_name 和 parent.first_name, parent.last_name
One way to go about this is using joins and table aliases .解决此问题的一种方法是使用连接和表别名。 Something like this:
像这样的东西:
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.