I have data for a game where in one table are the game stats and in another table is the game information like home team, away team, game date, and winner. I’m looking for a way to join the game info data with the stats data. The stats data is configured so that the first five players are the home team and the second five players are the away team.
game info schema:
gamelog( gameid, gamedate, hometeam, awayteam, winner )
gamestats( gameid, index, player, points, rebounds, … )
I would like my output to be:
- home, hometeam, player, points, rebound
- home, hometeam, player, points, rebound
- home, hometeam, player, points, rebound
- home, hometeam, player, points, rebound
- home, hometeam, player, points, rebound
- away, awayteam, player, points, rebound
- away, awayteam, player, points, rebound
- away, awayteam, player, points, rebound
- away, awayteam, player, points, rebound
- away, awayteam, player, points, rebound
I can’t seem to find a way to do a join so that the team column has its first five names as the home team and the second five names as the away team. The only way I have managed to do it is with two additional columns each with ten team names in them ie:
SELECT gamelog.hometeam, gamelog.awayteam, gamestats.player, gamestats.points, gamestats.rebounds
WHERE gamestats.id = 18873
AND gamelog.id = gamestats.id;
- hometeam, awayteam, player, points, rebound
- hometeam, awayteam, player, points, rebound
- hometeam, awayteam, player, points, rebound
- hometeam, awayteam, player, points, rebound
- hometeam, awayteam, player, points, rebound
- hometeam, awayteam, player, points, rebound
- hometeam, awayteam, player, points, rebound
- hometeam, awayteam, player, points, rebound
- hometeam, awayteam, player, points, rebound
- hometeam, awayteam, player, points, rebound
I guess another possibility would be to do multiple queries through PHP and then combine the data after the queries are done, though I am not sure if that would be more efficient. I could also add a column into the gamestats table that has the team names, but that seems redundant.
Edit: Posted the schema’s and cleared up the language
I figured it out. Had to do a join the first five rows of the gamestats table with the gamelog table home team name and then UNION the second five rows of the gamestats table with the game log table away team name.