有条件地从MySQL加入第二行

时间:2013-01-29 01:34:23

标签: mysql sql join

我有以下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 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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

我认为您也想要其他信息。

答案 2 :(得分: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 Fiddle

祝你好运。

答案 3 :(得分:0)

开始:

  • 步骤1:将第一列作为第五列,将第二列作为第六列。
  • 第2步:在位置列上使用IF来决定要显示的表达式