我正在使用这个MySql查询来创建一个联赛表,它运行得很好。我遇到的问题是这个查询仅适用于一个季节。当一个新的赛季开始时,如果不影响之前的赛季表,我就无法进入新赛季。
我有一个season表,其中包含:seasonID和seasonName 我有一个fixtures表,其中包含:matchID,seasonID,matchDate,matchHomeID,matchAwayID,matchHomeGoals,matchAwayGoals,matchAttendance * 我有一个团队表,其中包含:teamID,team,stadium **
* matchAttendance字段不是此查询的一部分。 **体育场字段不是此查询的一部分。
所以基本上我希望能够选择在特定赛季中发生的比赛。
我的网站上的表格可以看到here
如你所见,我有一个包含三个季节名称的下拉框。显然它不起作用,但它让你知道我想要做什么。
这是查询。
SELECT team AS Teams, Sum(P) AS P,Sum(W) AS W,Sum(D) AS D,Sum(L) AS L,
SUM(WA) AS WA, SUM(DA) AS DA, SUM(LA) AS LA, SUM(F) as F,SUM(A) AS A,
SUM(FA) AS FA, SUM(AA) AS AA, SUM(GD) AS GD, SUM(Pts) AS Pts
FROM(
SELECT
matchHomeID Teams,
1 P,
IF(matchHomeGoals > matchAwayGoals,1,0) W,
IF(matchHomeGoals = matchAwayGoals,1,0) D,
IF(matchHomeGoals < matchAwayGoals,1,0) L,
IF(matchHomeGoals > matchAwayGoals,0,0) WA,
IF(matchHomeGoals = matchAwayGoals,0,0) DA,
IF(matchHomeGoals < matchAwayGoals,0,0) LA,
matchHomeGoals F,
matchAwayGoals A,
0 FA,
0 AA,
matchHomeGoals-matchAwayGoals GD,
CASE WHEN matchHomeGoals > matchAwayGoals THEN 3 WHEN matchHomeGoals = matchAwayGoals THEN 1 ELSE 0 END PTS
FROM au_fixtures
UNION ALL
SELECT
matchAwayID,
1 P,
IF(matchHomeGoals < matchAwayGoals,0,0),
IF(matchHomeGoals = matchAwayGoals,0,0),
IF(matchHomeGoals > matchAwayGoals,0,0),
IF(matchHomeGoals < matchAwayGoals,1,0) WA,
IF(matchHomeGoals = matchAwayGoals,1,0) DA,
IF(matchHomeGoals > matchAwayGoals,1,0) LA,
0,
0,
matchAwayGoals FA,
matchHomeGoals AA,
matchAwayGoals-matchHomeGoals GD,
CASE WHEN matchHomeGoals < matchAwayGoals THEN 3 WHEN matchHomeGoals = matchAwayGoals THEN 1 ELSE 0 END
FROM au_fixtures
) as tot
JOIN au_teams t ON tot.Teams=t.teamID
GROUP BY Teams
ORDER BY SUM(Pts) DESC
答案 0 :(得分:0)
您需要在查询中包含SeasonId,以便Where SeasonId=season_you_want
在查询中的某个位置,可能在扫描查询后Group By
之前。
重新阅读后,您可能希望在season表中对季节名称执行where
并加入到season表中。