2012-11-17 126 views
1

的主键,所以我有两个表两个外键引用另一个表

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去这个

+0

您可能想纠正您的标题,这是误导,因为我们不是在这里谈论外键。 – Perception

+0

那么,你有什么尝试? –

+0

你有任何其他表格输入孩子的数据吗? – polin

回答

2

一种方法是使用joinstable aliases。类似这样的:

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 
相关问题