我有以下 3 个表:
mysql> explain runners;
+-------------+------------------+
| Field | Type |
+-------------+------------------+
| runner_id | int(10) unsigned |
| name | varchar(100) |
+-------------+------------------+
mysql> explain runs;
+-------------+------------------+
| Field | Type |
+-------------+------------------+
| run_id | int(10) unsigned |
| runner_id | int(10) unsigned |
| race_id | int(10) unsigned |
| position | int(10) |
+-------------+------------------+
mysql> explain races;
+-------------+------------------+
| Field | Type |
+-------------+------------------+
| race_id | int(10) unsigned |
| date | datetime |
+-------------+------------------+
接下来,我必须显示给定跑步者参加的所有比赛,如下所示:
mysql> select runner.name, runs.run_id, runs.position, races.race_id
from runners
join runs on(runs.runner_id = runners.runner_id)
join races on(races.race_id = run.run_id)
where runners.runner_id = 1;
+------------------+--------+----------+---------+
| name | run_id | position | race_id |
+------------------+--------+----------+---------+
| John Runner | 1 | 1 | 1 |
| John Runner | 7 | 3 | 2 |
| John Runner | 15 | 2 | 3 |
................... more lines ...................
+------------------+--------+----------+---------+
现在我应该在这个查询中添加另一个名为“win/second”的列,如果当前跑步者的位置 = 1,它将显示第二名的跑步者的名字,如果当前跑步者的位置,则显示该比赛中获胜跑步者的名字!= 1,但我只是不知道如何让它工作,甚至不知道从什么开始。