我有以下表格:如何加入這些表中的SQL
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 |
我有以下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;
及以下的輸出:
match_id| rival_team | name | last_name | substitution_id
--------+------------+----------+-----------+-----------
1 | chelsea fc | John | Doe | 1
1 | real madrid| John | Doe | 1
(2 rows)
然而,我想輸出像
match_id| rival_team | name | last_name | substitution_id
--------+------------+----------+-----=-----+----------------
1 | chelsea fc | John | Doe | 1
2 | real madrid| John | Doe | NULL
顯示John Doe被列入名單的所有比賽。無論玩家是否參與替換,我想要列名稱和替換ID號
我想我可以使用JOINS來實現這一點,但我不知道如何連接表,所以我可以有類似的東西到上面的輸出。我嘗試了很多聲明我有錯誤。
你得到了什麼錯誤? –
在你的例子中,'match_id'應該爲'real madrid'的1嗎?即'chelsea fc'正在match_id = 1播放'真正的馬德里'。 –
錯誤類似於「有表格的條目」,但它不能從查詢的這部分引用「...但我沒有明確地說出它因爲我真的不知道如何加入工作。我期待一些指導 – c2J