2015-09-23 53 views
1

我有一個包含這樣Postgres的錶行選擇

team_id | name | competition_id 
--------+---------+------ 
1  | name 1 | 10 
2  | name 2 | 10 

列的表的團隊和我也有這個表燈具

fixture_id | home_team_id | away_team_id | competition_id 
    --------+--------------+--------------+-------------- 
    1  | 1   | 2   | 10 
    2  | 2   | 1   | 10 
爲表燈具

主鍵是fixture_id,home_team_id,away_team_id , 和competition_id。 home_team,away_team和競爭也是外鍵。

如何從燈具中選擇home_team_name,away_team_name。

+0

問題是什麼? – frlan

+0

如何使用join或where子句選擇home_team_name,away_team_name? –

回答

2

您可以加入在teamsfixtures表兩次 - 一個是主隊,一個用於客隊:

SELECT fixture_id, home.name, away.name 
FROM fixtures f 
JOIN teams home on home.team_id = f.home_team_id 
JOIN teams away on away.team_id = f.way_team_id 
+0

謝謝你的工作! –