帮助MySQL SELECT语句,多个表

时间:2010-02-21 23:43:21

标签: mysql

我有以下表格:

reg_season

+--------+--------+---------+------+--------+
| season | league | team    | wins | losses |
+--------+--------+---------+------+--------+
| 1962   | AL     | Yankees | 96   | 66     |
+--------+--------+---------+------+--------+

postseason

+--------+---------+----------+
| season | team    | finish   |
+--------+---------+----------+
| 1962   | Yankees | champion |
+--------+---------+----------+

mvp

+--------+--------+--------+
| season | league | winner |
+--------+--------+--------+
| 1962   | AL     | Mantle |
+--------+--------+--------+

rookie_of_the_year

+--------+--------+--------+
| season | league | winner |
+--------+--------+--------+
| 1962   | AL     | Tresh  |
+--------+--------+--------+

每个表格包含每个季节的单个条目。

我需要一个查询来生成每个季节的摘要信息,即:

+--------+--------+------------------+--------------------+--------+-------+
| season | league | reg_season_champ | world_series_champ | mvp    | roy   |
+--------+--------+------------------+--------------------+--------+-------+
| 1962   | AL     | Yankees          | Yankees            | Mantle | Tresh |
+--------+--------+------------------+--------------------+--------+-------+
| 1961   | ...    | ...              | ...                | ...    | ...   |
+--------+--------+------------------+--------------------+--------+-------+
| 1960   | ...    | ...              | ...                | ...    | ...   |
+--------+--------+------------------+--------------------+--------+-------+

请指出正确的方向。感谢。

2 个答案:

答案 0 :(得分:1)

您正在寻找的是标准的内部联接。

答案 1 :(得分:0)

Select 
    r.season, 
    r.league, 
    r.team as reg_season_champ,
    p.team as world_series_champ,
    m.winner as mvp,
    y.winner as roy
from
    reg_season r
    inner join postseason p on r.season = p.season and r.league = p.league and p.finish = 'champion'
    inner join mvp m on r.season = m.season and r.league = m.league
    inner join rookie_of_the_year y on r.season = y.season and r.league = y.league