I’m working on a stat tracking webpage for a virtual hockey league on Xbox. I have been working on this for a bit, and have most of it done, but have realized an issue when displaying certain information. It seems to select the first row in the table that matches my JOIN, instead of selecting it by the WHERE criteria also. What I am trying to do is have a page that will display the box score of a game.
IE, from the info below, Game 1 would be ‘New York at Chicago’, and the final result should be NY-5, CHI-3. That works. The problem I’m running into is when I would display game 3, ‘Chicago at New York’, it shows the same stats, instead of CHI-2 NY-4.
Is there something I’m missing here, or is there a better way to do this? I basically want my JOIN to be doing the following..which is what I thought I was doing with the WHERE line.
JOIN GameStats GSA ON ((GSA.team_ID = gs.awayTeam_ID) AND (GSA.gameSchedule_ID = '" . $scheduleID . "')
JOIN GameStats GSH ON ((GSH.team_ID = gs.homeTeam_ID) AND (GSH.gameSchedule_ID = '" . $scheduleID . "')
Any help would be greatly appreciated!
$sql="SELECT
away.name as AWAY_TEAMNAME,
away.ID as AWAY_TEAMID,
home.name as HOME_TEAMNAME,
home.ID as HOME_TEAMID,
gs.ID as GAMEID
GSA.GOALS AS AWAY_GOALS,
GSA.SHOTS AS AWAY_SHOTS
GSH.GOALS AS HOME_GOALS,
GSH.SHOTS AS HOME_SHOTS
FROM
GameSchedule gs
JOIN Team away ON away.ID = gs.awayTeam_ID
JOIN Team home ON home.ID = gs.homeTeam_ID
JOIN GameStats GSA ON GSA.team_ID = gs.awayTeam_ID
JOIN GameStats GSH ON GSH.team_ID = gs.homeTeam_ID
WHERE
gs.ID = " . $scheduleID;
Table: Team
-------------------
| ID | name |
-------------------
| 1 | New York |
| 2 | Chicago |
| 3 | Detroit |
| 4 | Boston |
-------------------
Table: GameStats
--------------------------------------------------
| ID | gameSchedule_ID | team_ID | goals | shots |
--------------------------------------------------
| 1 | 1 | 1 | 5 | 5 |
| 2 | 1 | 2 | 3 | 2 |
| 3 | 2 | 3 | 6 | 1 |
| 4 | 2 | 4 | 1 | 7 |
| 5 | 3 | 2 | 2 | 14 |
| 6 | 3 | 1 | 4 | 7 |
| 7 | 4 | 4 | 5 | 9 |
| 8 | 4 | 3 | 5 | 4 |
-------------------------------------------------
Table: GameSchedule
----------------------------------
| ID | awayTeam_ID | homeTeam_ID |
----------------------------------
| 1 | 1 | 2 |
| 2 | 3 | 4 |
| 3 | 2 | 1 |
| 4 | 4 | 3 |
| 5 | 2 | 4 |
| 6 | 1 | 3 |
----------------------------------
Your query is selecting multiple rows, and your application is only showing the first row. It so happens that the first row is ordered by the games, but this is not necessarily so.
As you say in the question itself, the problem is the join on the game statistics. Your
fromclause should look like: