简体   繁体   English

如何在SQL中联接这些表

[英]How to join these tables in SQL

I have the following tables: 我有以下表格:

   match

 id   |   rival_team
----------------------
|  1  |   chelsea fc
|  2  |   real madrid


   player

 ID   |   name    |   last_name  |
---------------------------------
|  1  |    John   |      Doe     |
|  2  |   Peter   |   Williams   |


   called_up_players  

 match_id   |   player_id  |  substitution_id  |    
---------------------------|-------------------|
|     1     |      1       |         1         |
|     1     |      2       |        NULL       |


   substitution

|     id    |   match_id   | substitute_player_id | replaced_player_id |
---------------------------|----------------------|--------------------|
|     1     |      1       |           1          |          2         |

I have the following SQL statement 我有以下SQL语句

SELECT called_up_players.match_id, match.rival_team, player.name, 
           player.last_name, substitution.id
FROM called_up_players, substitution, player, match
WHERE called_up_players.substitution_id = substitution.substitute_player_id
         AND player.id = called_up_players.substitution_id;

and the following output: 和以下输出:

match_id| rival_team |   name   | last_name | substitution_id
--------+------------+----------+-----------+-----------
      1 | chelsea fc |   John   |    Doe    |         1
      1 | real madrid|   John   |    Doe    |         1
(2 rows)

However, I want the output to be like 但是,我希望输出像

match_id| rival_team |   name   | last_name | substitution_id
--------+------------+----------+-----=-----+----------------
      1 | chelsea fc |   John   |    Doe    |         1
      2 | real madrid|   John   |    Doe    |        NULL

to display all matches where John Doe was called up to the roster. 显示所有被召集到John Doe名单上的比赛。 Regardless the player was involved in a substitution, I would like to have the columns for the name and substitution_id 无论球员参与换人,我都希望有名称和替换ID的列

I think I can achieve this using JOINS , but I don't know how to join tables so I can have something similar to the above output. 我想我可以使用JOINS来实现这一点,但是我不知道如何联接表,因此可以得到与上述输出类似的东西。 I tried many statement I got errors. 我尝试了很多声明我出错了。

You are joining to match without a join condition, so this becomes a cross join not an inner join. 您要加入匹配项而没有加入条件,因此这将成为交叉联接,而不是内部联接。

This is one example of why a different join syntax is considered best practice: 这是为什么将不同的联接语法视为最佳实践的一个示例:

FROM called_up_players  
INNER JOIN substitution ON called_up_players.substitution_id = substitution.substitute_player_id  
 INNER JOIN player ON player.id = called_up_players.substitution_id  
 INNER JOIN match ON ???

thanks to Learning2Code and his answer I understood a bit more about joins and finally, I came up with an answer to get the output I wanted. 多亏了Learning2Code和他的回答,我对联接有了更多的了解,最后,我想出了一个获得所需输出的答案。

Instead of using INNER JOIN, I use LEFT JOIN so that, all records are retrieved from the table on the left and if there is no data from the table on the right, the field is just empty 我不使用INNER JOIN,而是使用LEFT JOIN,以便从左侧的表中检索所有记录,如果右侧的表中没有数据,则该字段为空

SELECT match_id, rival_team, name, last_name, substitution_id        
FROM match  
LEFT JOIN called_up_players ON called_up_players.match_id = match.id
LEFT JOIN player ON player.id = called_up_player.player_id
LEFT JOIN substitution ON called_up_players.substitution_id = substitution.substitution_id
WHERE player.name = 'John' AND player.last_name = 'Doe';

Output: 输出:

   partido_id | contrario  |  nombre    | apellido  | cambio_id
 -------------+------------+------------+-----------+------------
            1 | chelsea fc | John       | Doe       |         1
            2 | real madrid| John       | Doe       |
 (2 rows)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM