I’m working on a stat tracking webpage for a virtual hockey league on Xbox. I have my basic table layout setup, but I am struggling with how to create the query that will allow me to show the schedule. Below is my basic table layout, and what I’d like to do is have it display something like:
Game 1:
New York vs Chicago
Detroit vs Boston
Game 2:
Boston vs New York
Chicago vs Detroit
SELECT homeTeam.name AS HOME_TEAMNAME
FROM GameSchedule Sched
INNER JOIN GameStats GameStatsHome
ON Sched.ID = GameStatsHome.gameSchedule_ID
AND Sched.homeTeam_ID = GameStatsHome.team_ID
INNER JOIN Team homeTeam
ON homeTeam.ID = gameStatsHome.team_ID
I realize where I’m running into the problem is trying to pull the Team.name for both GameSchedule.awayTeam_ID and GameSchedule.homeTeam_ID. But I haven’t been able to figure out how to do this. I feel like I’m close..but then again, I have no idea what I’m doing, and have spent about 3 days trying to figure this one query out. I think if someone can point me in the right direction, I should be good for the rest of what I need… Any help would be greatly appreciated.
Table: Team
-----------------------------------------------------------
| ID | name | abbreviation | conference | division |
-----------------------------------------------------------
| 1 | New York | TMA | 1 | 1 |
| 2 | Chicago | TMB | 1 | 1 |
| 3 | Detroit | TMC | 1 | 1 |
| 4 | Boston | TMD | 1 | 1 |
-----------------------------------------------------------
Table: GameSchedule
------------------------------------------------
| ID | awayTeam_ID | homeTeam_ID | gameTime_ID |
------------------------------------------------
| 1 | 1 | 2 | 1 |
| 2 | 3 | 4 | 1 |
| 3 | 4 | 1 | 2 |
| 4 | 2 | 3 | 2 |
------------------------------------------------
Table: GameStats
------------------------------------------
| ID | gameSchedule_ID | team_ID | goals |
------------------------------------------
| 1 | 1 | 1 | 5 |
| 2 | 1 | 2 | 3 |
| 3 | 2 | 3 | 6 |
| 4 | 2 | 4 | 1 |
| 5 | 3 | 1 | 2 |
| 6 | 3 | 4 | 5 |
------------------------------------------
This will provide you both names
You can do whatever you want to in these joins!