MySQL INNER JOIN查询问题

时间:2016-10-17 13:37:39

标签: php mysql

我创建了一个INNER JOIN查询,如下所示,并想知道如何让它工作?我需要HomeTeam和AwayTeam在查询中等于TeamID。任何帮助将非常感激。感谢

$result = mysqli_query($con,"SELECT results.*,team.TeamName 
                             FROM results 
                                INNER JOIN team ON team.TeamID = results.HomeTeam 
                                INNER JOIN team on team.TeamID = results.AwayTeam");

1 个答案:

答案 0 :(得分:4)

您需要为包含两次的表使用别名。否则mysql无法区分这两者。

为了能够轻松处理结果,您可以对选择的名称执行相同的操作。

类似的东西:

SELECT 
    results.*, 
    t1.TeamName AS TeamNameHome,
    t2.TeamName AS TeamNameAway
FROM results 
INNER JOIN team t1
    ON t1.TeamID = results.HomeTeam 
INNER JOIN team t2
    ON t2.TeamID = results.AwayTeam