如何在条件中使用JOIN

时间:2019-05-15 00:00:24

标签: mysql sql

在MySQL的命令行中,我试图打印在单场比赛中进球超过3个的团队的名称,无论是主队,客队还是两者。 / strong>

我有两个关系:

Team
+---------+-----------+------+----+
| name    | shortName | abbr | id |
+---------+-----------+------+----+

Game
+---------+--------------+--------------+------------+------------+
| game_id | home_team_id | away_team_id | score_home | score_away |
+---------+--------------+--------------+------------+------------+

home_team_id away_team_id 都是 Team.id 的外键)

我首先尝试找到球队得分超过3个进球的game_id:

> SELECT game_id, score_home, score_away 
FROM Game 
WHERE score_home > 3 OR score_away > 3;
+---------+------------+------------+
| game_id | score_home | score_away |
+---------+------------+------------+
|       7 |          6 |          2 |
|      35 |          3 |          4 |
|      70 |          4 |          1 |
|      71 |          2 |          5 |
|      84 |          5 |          1 |
|      88 |          6 |          2 |
|      97 |          1 |          5 |
|     103 |          6 |          1 |
+---------+------------+------------+

所以我很确定最多只能有8支进球超过3个的球队。然后,我尝试了INNER JOINS,但是我不确定在两个不同的外键和条件下如何使用,但这是我的尝试:

> SELECT Team.name as Team_Name, Game.game_id, Game.score_home, Game.score_away 
    -> FROM Team
    -> INNER JOIN Game ON Team.id=home_team_id OR Team.id=away_team_id
    -> WHERE score_home > 3 OR score_away > 3;
+-------------------+---------+------------+------------+
| Team_Name         | game_id | score_home | score_away |
+-------------------+---------+------------+------------+
| Arsenal           |      71 |          2 |          5 |
| Everton           |       7 |          6 |          2 |
| Manchester City   |      70 |          4 |          1 |
| Manchester City   |      84 |          5 |          1 |
| Manchester City   |     103 |          6 |          1 |
| Norwich City      |      88 |          6 |          2 |
| Tottenham Hotspur |      70 |          4 |          1 |
| Tottenham Hotspur |      97 |          1 |          5 |
| Newcastle United  |      88 |          6 |          2 |
| Newcastle United  |     103 |          6 |          1 |
| West Ham United   |      35 |          3 |          4 |
| Leicester City    |      71 |          2 |          5 |
| Sunderland        |       7 |          6 |          2 |
| Bournemouth       |      35 |          3 |          4 |
| Bournemouth       |      84 |          5 |          1 |
| Bournemouth       |      97 |          1 |          5 |
+-------------------+---------+------------+------------+

当我只希望得分超过3分的球队时,这同时给了我主队和客队的名字。请帮忙。

3 个答案:

答案 0 :(得分:1)

使用UNION在子查询的一列中获取所有团队ID,然后将团队加入其中。

SELECT t.name
       FROM (SELECT g.home_team_id team_id
                    FROM game g
                    WHERE score_home > 3
             UNION
             SELECT g.away_team_id team_id
                    FROM game g
                    WHERE score_away > 3) x
            INNER JOIN team t
                       ON t.id = x.team_id;

答案 1 :(得分:0)

一种方法使用相关子查询:

select t.name
from team t
where exists (select 1
              from game g
              where g.home_team_id = t.id and
                    g.score_home > 3
             ) and
      exists (select 1
              from game g
              where g.away_team_id = t.id and
                    g.score_away > 3
             ) ;

此查询可以利用game(home_team_id, score_home)game(away_team_id, score_away)上的索引。

答案 2 :(得分:0)

如果在一次比赛中均得分超过3个进球,则以下查询将在单独的行中返回主场和客场-

SELECT 
T.Name as [Team_Name],
A.Team_Type,
A.Game_id,
A.Score
FROM
(
    SELECT 'Home' AS [Team_Type],game_id AS Game_id,home_team_id as team_ID, score_home as Score WHERE score_home>3
    UNION ALL
    SELECT 'Away' AS [Team_Type], game_id AS Game_id,away_team_id as team_ID, score_away as Score WHERE score_away>3
)A
INNER JOIN Team T 
ON T.id = A.team_ID
ORDER BY A.Game_id