2014-06-13 88 views
0

選擇多個相應的行您好,我有兩個表betting_match和betting_teams:從另一個表

enter image description here

,我想選擇比賽和也選擇相匹配的相應的團隊。

例如這樣的選擇數據:

ID | BetradarMatchId | betting_tournament_id | Team1 | Team2 
1 4     3      Barca Real 

其中巴薩和皇家來自betting_teams表。

我試圖使用查詢,但這是行不通的。

SELECT 
    matches.id, 
    matches.BetradarMatchId, 
    matches.betting_tournament_id, 
    teams.title as Team1, 
    teams.title as Team2 
FROM 
    betting_match matches 

LEFT JOIN 
    betting_teams teams 
ON 
    teams.id = matches.betting_teams_id 
    OR 
    teams.id = matches.betting_teams_id1 
WHERE 
    matches.status != 0 
     AND 
    matches.top = 1 
GROUP BY 
    matches.id 
+0

什麼是不使用它?是否有錯誤反饋或僅顯示錯誤數據?另外,爲什麼GROUP BY沒有聚合? – Utrolig

回答

0

你要使用兩個連接:

SELECT matches.id, matches.BetradarMatchId, matches.betting_tournament_id, 
     team.title as Team1, 
     team2.title as Team2 
FROM betting_match matches LEFT JOIN 
    betting_teams team 
    ON team.id = matches.betting_teams_id LEFT JOIN 
    betting_teams team2 
    ON team2.id = matches.betting_teams_id1 
WHERE matches.status <> 0 AND matches.top = 1;