如何创建进度图表(MySQL)

时间:2014-01-31 11:30:51

标签: mysql

我搜索了这个网站来解决我的问题,但找不到它。

我正在为本地手球联赛创建一个竞赛排名表。我创造了 两个看起来像这样的表:

teams:id int主键auto_increment,tname varchar 50

游戏:idmatch int主键auto_increment,matchday tinyint,date datetime,hteam int,ateam int,hscore tinyint,ascore tinyint

这是我的问题:

SELECT @rowno:= @rowno+1 as position, total.*
from (SELECT matchday,tname 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(Pts) AS Pts 
FROM( SELECT
matchday,hteam Team, 1 P,
IF(hscore > ascore,1,0) W,
IF(hscore = ascore,1,0) D,
IF(hscore < ascore,1,0) L,
hscore F,
ascore A,
CASE WHEN hscore > ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END PTS
FROM games
UNION ALL
SELECT
matchday,ateam, 1,
IF(hscore < ascore,1,0),
IF(hscore = ascore,1,0),
IF(hscore > ascore,1,0),
ascore,
hscore,
CASE WHEN hscore < ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END
FROM games) AS tot
JOIN teams t ON tot.Team=t.id
CROSS JOIN   (SELECT @rowno := 0) r
WHERE matchday <= 1
GROUP BY Team
ORDER BY  SUM(Pts) DESC, SUM(F) DESC)total
ORDER BY Pts DESC, F DESC

在戈登的帮助下,排名顺序正在发挥作用,但现在我无法弄清楚如何根据此查询创建进度图并创建类似this的内容。为了更清楚地说明我想要达到的目标,例如A队在比赛日之后的第一场和第三场比赛日之后的第二场和第三场比赛日之后。它给了我图表的值,所以在我的例子中,值应该是1,3和4.这是我的查询示例SQL Fiddle demo

希望有人能帮我找到解决方案。

2 个答案:

答案 0 :(得分:0)

只需将您的order by更改为order by sum(pts) desc

SELECT @rowno:= @rowno+1 as position, tname 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(Pts) AS Pts 
FROM (SELECT hteam Team, 1 P, IF(hscore > ascore,1,0) W,
             IF(hscore = ascore,1,0) D, IF(hscore < ascore,1,0) L,
             hscore F, ascore A,
             CASE WHEN hscore > ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END PTS
      FROM games
      UNION ALL
      SELECT ateam, 1, IF(hscore < ascore,1,0),
             IF(hscore = ascore,1,0), IF(hscore > ascore,1,0),
             ascore, hscore,
             CASE WHEN hscore < ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END
      FROM games
     ) AS tot JOIN
     teams t ON tot.Team=t.id CROSS JOIN
     (SELECT @rowno := 0) row
GROUP BY Team
ORDER BY SUM(Pts) DESC ;

在排序后发生变量计算。如果您需要原始订单,可以使用子查询:

select t.*
from (<your query here>) t
order by position ASC, Pts DESC;

编辑:

您的评论很奇怪,但可以重现。使用子查询可以解决问题:

select @rowno:= @rowno+1 as position, t.*
from (SELECT tname 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(Pts) AS Pts 
      FROM (SELECT hteam Team, 1 P, IF(hscore > ascore,1,0) W,
                   IF(hscore = ascore,1,0) D, IF(hscore < ascore,1,0) L,
                   hscore F, ascore A,
                   CASE WHEN hscore > ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END PTS
            FROM games
            UNION ALL
            SELECT ateam, 1, IF(hscore < ascore,1,0),
                   IF(hscore = ascore,1,0), IF(hscore > ascore,1,0),
                   ascore, hscore,
                   CASE WHEN hscore < ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END
            FROM games
           ) AS tot JOIN
           teams t ON tot.Team=t.id CROSS JOIN
           (SELECT @rowno := 0) row
      GROUP BY Team
     ) t
  ORDER BY Pts DESC ;

答案 1 :(得分:0)

然后只需将您的订单更改为此

    ORDER BY SUM(Pts) DESC;