0

我正在创建一个体育页面,但我已经达到了我自己无法解决的地步。

我的问题是我无法弄清楚如何在每个比赛日之后分别存储每支球队的位置。这是我想要实现的目标:

关联

为了创建排名表,我有两张表:

俱乐部:id_clubs,名称
结果:matchid,matchday,hometeam,awayteam,homegoals,awaygoals,date,id_league

从结果表中,我创建了如下设置的排名表:

SELECT
    name AS Team,
    Sum(P) AS P,
    Sum(W) AS W,
    Sum(D) AS D,
    Sum(L) AS L, 
    SUM(F) as F,
    SUM(A) AS A,
    SUM(GD) AS GD,
    SUM(Pts) AS Pts  
FROM ( 
    SELECT *,
        hometeamid Team,  
        1 P, 
        IF(homegoal > awaygoal,1,0) W, 
        IF(homegoal = awaygoal,1,0) D, 
        IF(homegoal < awaygoal,1,0) L, 
        homegoal F, 
        awaygoal A, 
        homegoal-awaygoal GD, 
        CASE WHEN homegoal > awaygoal THEN 3
             WHEN hometeam = awaygoal THEN 1
             ELSE 0
        END PTS 
    FROM results
    WHERE id_league = '$leagueid'
      AND homegoal IS NOT NULL
      AND awaygoal IS NOT NULL

    UNION ALL 

    SELECT *,
        awayteamid, 
        1, 
        IF(homegoal < awaygoal,1,0), 
        IF(homegoal = awaygoal,1,0), 
        IF(homegoal > awaygoal,1,0), 
        awaygoal, 
        homegoal, 
        awaygoal-homegoal GD, 
        CASE WHEN homegoal < awaygoal THEN 3
             WHEN homegoal = awaygoal THEN 1
             ELSE 0
        END 
        FROM results
        WHERE id_league = '$leagueid' 
          AND awaygoal IS NOT NULL
          AND homegoal IS NOT NULL
) AS tot 
JOIN clubs t ON tot.Team=t.id_clubs  
GROUP BY Team  
ORDER BY
    SUM(Pts) desc,
    SUM(GD) desc

希望有人可以提供一些提示或解决方案。

4

1 回答 1

0
于 2013-04-29T03:35:47.567 回答