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
https://forums.phpfreaks.com/topic/38476-solved-having-troubl-with-query-joins/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.