2017-05-19 47 views
0

我有兩個表...從一行加入兩個ID與另一個表

燈具

enter image description here

enter image description here

現在我想要顯示名字o f雙方隊伍(lteam和vteam)對陣 對方在一張表中。我試圖左加入這些表,但這是行不通的。猜你知道爲什麼?

SELECT * FROM fixtures 

LEFT JOIN teams as a ON fixtures.lteam = teams.id 
LEFT JOIN teams as b ON fixtures.vteam = teams.id 

WHERE date_ko = '2017-05-19' 

感謝您的幫助!

+0

拿什麼*,因爲我需要* – Jens

+1

變化**燈具不起作用。 lteam = teams.id **到** fixtures.lteam = a.id **和** b.id ** –

+0

與您的樣本數據您的查詢將永遠不會工作。 team.id = 6甚至沒有在lteam或vteam中。你怎麼認爲你可以加入這兩張桌子? –

回答

2

連接不正確。更改此:

LEFT JOIN teams as a ON fixtures.lteam = teams.id 
LEFT JOIN teams as b ON fixtures.vteam = teams.id 
              | 

要這樣:

LEFT JOIN teams as a ON fixtures.lteam = a.id 
LEFT JOIN teams as b ON fixtures.vteam = b.id 
             | 

您需要使用別名的加入也

+0

酷!謝謝,但我怎麼可以在我的foreach($ pdo-> query($ sql)中使用** a.id **和** b.id **作爲$ row)...''? '$ row ['a.id']'似乎不起作用。感謝您的支持! – vloryan

相關問題