执行联盟和加入

时间:2015-05-11 17:26:18

标签: mysql sql join

我在MySQL中有以下表格:

schedule

+-------------+---------------------+-----------+------------+-----------+------------+-------+-----------------+
| schedule_id | schedule_datetime   | team_home | score_home | team_away | score_away | venue | schedule_status |
+-------------+---------------------+-----------+------------+-----------+------------+-------+-----------------+
|           2 | 2015-05-05 20:00:00 |         4 |         75 |         5 |   64       |     1 |               2 |
|           3 | 2015-05-05 22:00:00 |         7 |         34 |         6 |   85       |     2 |               2 |
|           4 | 2015-05-05 21:00:00 |         2 |         74 |         1 |  101       |     1 |               2 |
|           5 | 2015-05-05 17:00:00 |         8 |         96 |         3 |   85       |     2 |               2 |
|           6 | 2015-05-06 16:00:00 |         4 |        105 |         3 |  101       |     2 |               2 |
|           7 | 2015-05-06 17:30:00 |         1 |         96 |         8 |   74       |     2 |               2 |
|           8 | 2015-05-06 17:00:00 |         6 |         82 |         2 |   90       |     1 |               2 |
|           9 | 2015-05-06 19:00:00 |         5 |         76 |         7 |  101       |     1 |               2 |
|          10 | 2015-05-07 16:00:00 |         4 |         85 |         6 |   92       |     1 |               2 |
|          11 | 2015-05-07 18:00:00 |         5 |         74 |         1 |   64       |     2 |               2 |
|          12 | 2015-05-07 19:00:00 |         7 |        101 |         3 |  113       |     1 |               2 |
|          13 | 2015-05-07 21:00:00 |         2 |         75 |         8 |  105       |     2 |               2 |
|          14 | 2015-05-08 16:00:00 |         4 |         64 |         7 |   36       |     1 |               1 |
|          15 | 2015-05-08 17:30:00 |         2 |         55 |         5 |   72       |     2 |               1 |
|          16 | 2015-05-08 17:00:00 |         8 |         80 |         6 |   91       |     1 |               1 |
|          17 | 2015-05-08 21:00:00 |         3 |        115 |         1 |   96       |     2 |               1 |
|          18 | 2015-05-09 16:00:00 |         4 |        104 |         8 |   74       |     1 |               2 |
|          19 | 2015-05-09 19:00:00 |         3 |        101 |         2 |  105       |     1 |               2 |
|          20 | 2015-05-09 15:00:00 |         1 |         94 |         7 |   80       |     2 |               2 |
|          21 | 2015-05-09 18:00:00 |         6 |        101 |         5 |  110       |     2 |               2 |
|          22 | 2015-05-10 18:00:00 |         4 |          0 |         1 |    0       |     1 |               0 |
|          23 | 2015-05-10 19:30:00 |         6 |          0 |         3 |    0       |     1 |               0 |
|          24 | 2015-05-10 19:00:00 |         5 |          0 |         8 |    0       |     2 |               0 |
|          25 | 2015-05-10 21:00:00 |         7 |          0 |         2 |    0       |     2 |               0 |
|          26 | 2015-05-11 18:00:00 |         4 |          0 |         2 |    0       |     1 |               0 |
|          27 | 2015-05-11 21:00:00 |         8 |          0 |         7 |    0       |     1 |               0 |
|          28 | 2015-05-11 18:00:00 |         3 |          0 |         5 |    0       |     2 |               0 |
|          29 | 2015-05-11 21:00:00 |         1 |          0 |         6 |    0       |     2 |               0 |
+-------------+---------------------+-----------+------------+-----------+------------+-------+-----------------+

teams

+---------+------------------------+---------------+
| team_id | team_name              | team_division |
+---------+------------------------+---------------+
|       1 | New York Knicks        |             1 |
|       2 | Brooklyn Nets          |             1 |
|       3 | Boston Celtics         |             1 |
|       4 | Philadelphia 76ers     |             1 |
|       5 | Denver Nuggets         |             4 |
|       6 | Minnesota Timberwolves |             4 |
|       7 | Oklahoma City Thunder  |             4 |
|       8 | Portland Trail Blazers |             4 |
|       9 | Utah Jazz              |             4 |
+---------+------------------------+---------------+

我尝试执行以下SQL(从以下问题修改:MySQL Volleyball Standings):

SELECT 
    team_id,
    COUNT(*) AS GP,
    SUM(is_win) AS Wins,
    SUM(NOT is_win) AS Losses
FROM
(
    SELECT
    team_home AS team_id,
    score_home > score_away AS is_win
    FROM schedule
    WHERE schedule_status = 2
    UNION ALL
    SELECT
    team_away AS team_id,
    score_away > score_home AS is_win
    FROM schedule
    WHERE schedule_status = 2
) T1
GROUP BY team_id
ORDER BY Wins DESC, Losses ASC

我希望能够从团队表中检索团队的名称,而不是通过团队的ID进行报告。但是,使用以下代码时,我不断收到错误消息。我知道我需要加入JOIN但是我在哪里修改代码以允许我一起加入结果?

2 个答案:

答案 0 :(得分:1)

假设您现有的查询提供了所需的结果,您只需要加入teams

SELECT 
    team_name,
    COUNT(*) AS GP,
    SUM(is_win) AS Wins,
    SUM(NOT is_win) AS Losses
FROM
(
    SELECT
    team_home AS team_id,
    score_home > score_away AS is_win
    FROM schedule
    WHERE schedule_status = 2
    UNION ALL
    SELECT
    team_away AS team_id,
    score_away > score_home AS is_win
    FROM schedule
    WHERE schedule_status = 2
) T1
LEFT JOIN teams  
  ON T1.team_id = teams.team_id
GROUP BY team_name
ORDER BY Wins DESC, Losses ASC

使用LEFT,但如果团队保证在团队表中,那么您可以使用INNER

答案 1 :(得分:1)

SELECT 
    T1.team_id,
    teams.team_name,
    COUNT(1) AS GP,
    SUM(T1.is_win) AS Wins,
    SUM(NOT T1.is_win) AS Losses
FROM
(
    SELECT
    team_home AS team_id,
    score_home > score_away AS is_win
    FROM schedule
    WHERE schedule_status = 2
    UNION ALL
    SELECT
    team_away AS team_id,
    score_away > score_home AS is_win
    FROM schedule
    WHERE schedule_status = 2
) T1
INNER JOIN teams ON T1.team_id = teams.team_id
GROUP BY T1.team_id
ORDER BY Wins DESC, Losses ASC
;

一般来说,您可以将子查询视为任何其他表。我一般说,因为TEMPORARY表有一些奇怪的限制,偶尔UNIONs似乎不喜欢某些情况。