Mysql体育联赛表

时间:2013-04-28 09:39:41

标签: mysql graph progress storing-data

我正在创建一个体育页面,我已经达到了一个我无法解决的问题。

我的问题是我无法弄清楚如何在每个比赛日之后将每个队伍的位置分开存放。这就是我想要实现的目标:

link

为了创建联赛表,我有两张桌子:

俱乐部: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

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

1 个答案:

答案 0 :(得分:0)

如果您有关从结果表格中检索订单号的问题,请参阅以下类似的讨论:MySQL - Get row number on select

要选择第一个n个匹配项,您可以在按匹配ID分组后执行LIMIT n