如何根据团队表中的两个外键在查询,飞行员和副驾驶员中选择每个团队成员的姓名
表格:玩家
+----------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------------+-----+---------+----------------+
| id | int(10) unsigned| NO | PRI | NULL | auto_increment |
| name | varchar(60) | NO | | NULL | |
| isPilot | TINYINT(1) | NO | | NULL | |
| age | int(4) | NO | | NULL | |
+----------------+-----------------+------+-----+---------+----------------+
表格:团队
+----------------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-----------------+------+-----+---------+----------------+
| id | int(10) unsigned| NO | PRI | NULL | auto_increment |
| pilot_id | int(10) unsigned| NO | FK | NULL | |
| copilot_id | int(10) unsigned| NO | FK | NULL | |
+----------------+-----------------+------+-----+---------+----------------+
这是一个很好的数据库设置吗?
可能是SQL: Foreign Key With multiple child values的转发,但不能应用于我的
MCVE:https://www.db-fiddle.com/f/a2fCdy6RFqgReuL8FThhDP/2 结果应该是类似
+----------------+-----------------+
| Pilot | Co-Pilot |
+----------------+-----------------+
| player4Name | player3Name |
| player2Name | player1Name |
+----------------+-----------------+
答案 0 :(得分:1)
似乎您需要两个简单的JOIN:
SELECT
p.name AS pilot,
cp.name AS copilot
FROM
team AS t
INNER JOIN player AS p on p.id = t.pilot_id
INNER JOIN player AS cp on cp.id = t.copilot_id
答案 1 :(得分:1)