MySQL: 在单条记录上选择多个外键字段
MySQL: Selecting several foreign key fields on a single record
在MySQL(InnoDB)中,我创建了两个属于历史字符列表的相关表:
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'应该包含信息数据,例如'Unknown','Documentally proven', 'Speculative' 等等。
我想要实现的是在 'characters' 上执行 SELECT 语句,以便 '*_checking' int 值被它们各自的 'checktype' 文本替换(可能不同, 当然)。
在此先感谢您的帮助。
诀窍是加入 'checking' 两次,如下所示:
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
仅作记录,更紧凑的版本应该是:
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
在MySQL(InnoDB)中,我创建了两个属于历史字符列表的相关表:
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'应该包含信息数据,例如'Unknown','Documentally proven', 'Speculative' 等等。
我想要实现的是在 'characters' 上执行 SELECT 语句,以便 '*_checking' int 值被它们各自的 'checktype' 文本替换(可能不同, 当然)。
在此先感谢您的帮助。
诀窍是加入 'checking' 两次,如下所示:
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
仅作记录,更紧凑的版本应该是:
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