2014-06-13 61 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;