Kyle Kyle - 1 month ago 8
MySQL Question

MySQL INNER JOIN query issue

I have created an INNER JOIN query as shown below and was wondering how I can make it work? I need for the HomeTeam and AwayTeam to equal TeamID in the query. Any help would be much appreciated. Thanks

$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");

Answer

You need to use aliases for the table that you are including twice. Otherwise mysql cannot distinguish between the two.

To be able to process the results easily, you can do the same with the names you are selecting.

Something like:

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
Comments