Jump to content

[SOLVED] having troubl with query joins


chiprivers

Recommended Posts

I have a table (teams) that contains a list of football teams:

 

teamID      int

team        varchar

 

and a further table (fixtures) that contains a list of fixtures:

 

fixtureID    int

date        date

home        int (refers to teamID in teams table)

away        int (refers to teamID in teams table)

 

I would like to query the fixtures table to get fixtures of a specific date (easy enough) but would also like to return the team name of both the home and away teams.

 

At the moment I have:

 

SELECT home, away FROM fixtures WHERE date = $date

 

this will obviously only return the teamID values, how do I add a join to get the team names from the teams table at the same time instead of getting the ID's?

Link to comment
Share on other sites

You'll need to JOIN in the teams table twice:

 

SELECT f.*, h.team AS homeTeam, a.team AS awayTeam 
FROM fixtures AS f 
LEFT JOIN teams AS h ON ( h.teamID = f.home ) 
LEFT JOIN teams AS a ON ( a.teamID = f.away ) 
WHERE f.date = '$date'

 

Hope that helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.