2015-01-13 17 views
0

在MySQL(InnoDB的),我已经创建了属于历史人物列表两个关系表:MySQL的:上一个记录选择几家外资重点领域

characters: 
----------------------------- 
character_id (PK): int 
name:     varchar 
birthplace:   varchar 
birthplace_checking: int 
birthdate:   date 
birthdate_checking: int 
------------------------------ 

checking: 
------------------------------ 
checking_id (PK):  int 
checktype:   varchar 
------------------------------ 

其中“birthplace_checking”和“birthdate_checking”是国外提及'checking_id'和'checktype'的键应包含信息数据,如'未知','文档证明','推测'等等。

我想要实现的是在'characters'上执行一个SELECT语句,以便'* _checking'int值被它们各自的'checktype'文本(当然可能不同)取代。

在此先感谢您的帮助。

回答

1

诀窍是连接到“检查”的两倍,如下图所示:

select c.name 
    , c.birthplace 
    , bplace.checktype 
    , c.birthdate 
    , bdate.checktype 
from characters c 
join checking bplace 
      on c.birthplace_checking = bplace.checking_id 
join checking bdate 
      on c.birthdate_checking = bdate.checking_id 
+0

点上!非常感谢你! –

0

只是为了记录在案,更紧凑的版本应该是:

select c.name 
    , c.birthplace 
    , bplace.checktype 
    , c.birthdate 
    , bdate.checktype 
from characters c, checking bplace, checking bdate 
where c.birthplace_checking = bplace.checking_id 
    and c.birthdate_checking = bdate.checking_id