我正在为Xbox上的虚拟冰球联盟创建一个统计跟踪网页。我一直在研究这个问题,并且已经完成了大部分工作,但在显示某些信息时却意识到了这个问题。它似乎选择表中与我的JOIN匹配的第一行,而不是通过WHERE标准选择它。我想要做的是有一个页面显示游戏的盒子分数。
IE,从下面的信息来看,第一场比赛将是'芝加哥纽约',最终结果应该是NY-5,CHI-3。这样可行。我遇到的问题是当我展示第3场比赛时,'芝加哥在纽约',它显示相同的统计数据,而不是CHI-2 NY-4。
我在这里缺少什么,或者有更好的方法吗?我基本上希望我的JOIN能够执行以下操作......这就是我认为我在使用WHERE行的原因。
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 . "')
非常感谢任何帮助!
$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 |
----------------------------------
答案 0 :(得分:1)
您的查询正在选择多行,而您的应用程序仅显示第一行。碰巧第一行是由游戏排序的,但不一定如此。
正如你在问题中所说,问题在于加入游戏统计数据。您的from
子句应如下所示:
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 and
gsa.gameschedule_id = gs.id
JOIN GameStats GSH ON GSH.team_ID = gs.homeTeam_ID and
gsh.gameschedule_id = gs.id