1

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");
3
  • Well with sort of thing we need to know where we want to go and from where we need to start from... So can you put your tables in here and also a sample of what you are expecting to achieve? That would help us work something out. Commented Oct 17, 2016 at 13:40
  • show us your tables please. Commented Oct 17, 2016 at 13:40
  • prntscr.com/cve52o Commented Oct 17, 2016 at 13:43

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, I shall give this a bash!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.