4

我有以下 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,但我只是不知道如何让它工作,甚至不知道从什么开始。

4

4 回答 4

1

这将获得每场比赛的第一名和第二名。

SELECT r1.race_id,
       r1.runner_id AS First,
       r2.runner_id AS Second
FROM   runs r1
       INNER JOIN
       runs r2 ON r1.race_id=r2.race_id AND r1.position=1 AND r2.position=2

你可以把它加入你的其他桌子以获得你想要的任何东西

好的 - 扩展。这是 SQL 服务器语法 - 它可能需要一些翻译

select runner.name,
       runs.run_id, 
       runs.position, 
       races.race_id,
       case runs.position
            when 1 then pos.Second
            else pos.First
       end AS [First/Second] 
from runners
     inner join runs on runs.runner_id = runners.runner_id
     inner join races on races.race_id = run.run_id
     inner join (SELECT r1.race_id,
                        r1.runner_id AS First,
                        r2.runner_id AS Second
                 FROM   runs r1
                 INNER JOIN
                 runs r2 ON r1.race_id=r2.race_id AND r1.position=1 AND r2.position=2) as pos
where runners.runner_id = 1;

或者,简化

select runner.name,
       r1.run_id, 
       r1.position, 
       r1.race_id,
       r3.name AS [First/Second]
from runners
     inner join runs r1 on r1.runner_id = runners.runner_id
     inner join (SELECT runners2.name
                 FROM runners runners2
                 INNER JOIN
                 runs r2 ON r1.race_id=r2.race_id 
                         AND r2.position=CASE r1.position WHEN 1 THEN 2 ELSE 1 END) r3
where runners.runner_id = 1;
于 2013-01-29T01:39:28.397 回答
1

以下查询通过预先计算第一个和第二个地名,然后将它们加入来解决这个问题:

select runner.name, runs.run_id, runs.position, races.race_id,
       (case when runs.position = 1 then SecondRunner else FirstRunner end) as FirstOrSecond
from runners join
     runs
     on runs.runner_id = runners.runner_id join
     races
     on races.race_id = run.run_id join
     (select runs.race_id,
             max(case when position = 1 then runners.name end) as FirstRunner,
             max(case when position = 2 then runners.name end) as SecondRunner
      from runs join
           runners
           on runs.runner_id = runners.runner_id
    ) fs
    on fs.race_id = races.race_id
where runners.runner_id = 1

如果你想要简单,那么只需使用子查询:

     select runs.race_id,
             max(case when position = 1 then runners.name end) as FirstRunner,
             max(case when position = 2 then runners.name end) as SecondRunner
      from runs join
           runners
           on runs.runner_id = runners.runner_id

我以为你也想要额外的信息。

于 2013-01-29T01:42:08.397 回答
1

这应该可以满足您的需求:

select runners.name, 
   runs.run_id, 
   runs.position, 
   races.race_id,
   case
      when runs.position = 1
      then    runnerssecond.Name
      else    runnerswinner.Name
   end as NextColumn
from runners
   join runs on(runs.runner_id = runners.runner_id)
   join races on(races.race_id = runs.Race_Id)
   left join runs runswinner on runswinner.race_id = races.race_id and runswinner.Position = 1
   left join runners runnerswinner on runnerswinner.Runner_Id = runswinner.Runner_Id
   left join runs runsecond on runsecond.race_id = races.race_id and runsecond.Position = 2
   left join runners runnerssecond on runnerssecond.Runner_Id = runsecond.Runner_Id
where runners.runner_id = 1;

还有SQL 小提琴

祝你好运。

于 2013-01-29T02:04:12.270 回答
0

开始:

  • 第 1 步:将第一个作为第五列,将第二个作为第六列。
  • 第 2 步:在位置列上使用 anIF来决定显示哪个表达式
于 2013-01-29T01:38:36.087 回答