使用mysql中的case和limit计算/求和结果

时间:2015-03-19 20:43:39

标签: mysql

尝试创建一个查询,让我获得总赢,抽奖和亏损。我有以下查询

SELECT CASE 
    WHEN m.home_team = '192'
        AND m.home_full_time_score > m.away_full_time_score
        OR m.away_team = '192'
        AND m.home_full_time_score < m.away_full_time_score
        THEN 1
    END AS 'wins',
CASE 
    WHEN m.home_team = '192'
        AND m.home_full_time_score = m.away_full_time_score
        OR m.away_team = '192'
        AND m.home_full_time_score = m.away_full_time_score
        THEN 1
    END AS 'draws',
CASE 
    WHEN m.home_team = '192'
        AND m.home_full_time_score < m.away_full_time_score
        OR m.away_team = '192'
        AND m.home_full_time_score > m.away_full_time_score
        THEN 1
    END AS 'loss'
FROM exp_tm_matches m
INNER JOIN exp_tm_division_matches dm
    ON (dm.match = m.id)
INNER JOIN exp_tm_divisions d
    ON (dm.division = d.id)
WHERE m.info = 'PL'
    AND (
        m.home_team = '192'
        OR m.away_team = '192'
        )
    AND dm.season = '16'
ORDER BY m.kick_off DESC LIMIT 5

这会产生以下内容

wins        draws       loses   
NULL        1           NULL
1           NULL        NULL
1           NULL        NULL
Null        1           NULL
1           NULL        NULL

我想要得到的是Wins = 3 Draws = 2 Loses = 0,如果我在case语句中添加count它忽略了5的LIMIT。

4 个答案:

答案 0 :(得分:1)

使用条件聚合。你快到了:

SELECT SUM( (m.home_team = 192 AND m.home_full_time_score > m.away_full_time_score) OR
            (m.away_team = 192 AND m.home_full_time_score < m.away_full_time_score
            )
          ) as wins,
       SUM(m.home_full_time_score = m.away_full_time_score) 
          ) as draws,
       SUM( (m.home_team = 192 AND m.home_full_time_score < m.away_full_time_score) OR
            (m.away_team = 192 AND m.home_full_time_score > m.away_full_time_score
            )
          ) as losses
FROM exp_tm_matches m INNER JOIN
     exp_tm_division_matches dm
     ON dm.match = m.id
WHERE m.info = 'PL' AND
      192 IN (m.home_team, m.away_team) AND
      dm.season = 16;

注意:

  • 您不使用divisions表,因此我删除了该连接。
  • 如果id是数字,则不应在常量周围使用引号。
  • 您不应对列别名使用单引号。这些名称不需要任何转义字符,因此我删除了它们。实际上,只对字符串和日期常量使用单引号。
  • MySQL有一个很好的功能,布尔值可以被视为0表示false,1表示true表示,无需if()case
  • draws的逻辑不需要查看团队代码。
  • 您只需要一行返回摘要信息,因此limit是不必要的。

要获取最后五场比赛,请将fromwhere条款更改为:

FROM (SELECT m.*
      FROM exp_tm_matches m INNER JOIN
           exp_tm_division_matches dm
           ON dm.match = m.id
      WHERE m.info = 'PL' AND
            192 IN (m.home_team, m.away_team) AND
            dm.season = 16
      ORDER BY m.kick_off DESC
      LIMIT 5
     ) m

答案 1 :(得分:0)

您还需要在AND OR AND语句中添加一些括号,因为它不会以这种方式工作。

   WHERE m.home_team = '192'
    AND m.home_full_time_score < m.away_full_time_score
    OR m.away_team = '192'
    AND m.home_full_time_score > m.away_full_time_score

结果:主场或客场球队得分。

可能效果更好:

    WHERE (m.home_team = '192'
    AND m.home_full_time_score < m.away_full_time_score)
    OR (m.away_team = '192'
    AND m.home_full_time_score > m.away_full_time_score)

结果如下:主队得分<客场OR(主场比赛时)客队有得分&gt;远

答案 2 :(得分:0)

可能的解决方案如下:

SELECT SUM(wins) as total_wins,
 SUM(loss) as total_losses,
 SUM(draws) as total_draws
FROM
( 
...add your complete query here, but you ahve to add a column we can group the results by 
i.e.: SELECT m.home_team AS team
) temp
GROUP BY team

希望有所帮助

答案 3 :(得分:0)

我不得不做类似的事情。我不是一个沉重的开发类型的人,但这对我很有用。我根据我的旧查询修改了查询的第一位。它适用于我创建的样本数据

SELECT 'wins' = sum(CASE 
WHEN m.home_team = '192'
    AND m.home_full_time_score > m.away_full_time_score
    OR m.away_team = '192'
    AND m.home_full_time_score < m.away_full_time_score
    THEN 1 else 0 end)
,  

'draws' = sum(CASE 
WHEN m.home_team = '192'
    AND m.home_full_time_score = m.away_full_time_score
    OR m.away_team = '192'
    AND m.home_full_time_score = m.away_full_time_score
    THEN 1 else 0 end)
,  

'loss' = sum(CASE 
WHEN m.home_team = '192'
    AND m.home_full_time_score < m.away_full_time_score
    OR m.away_team = '192'
    AND m.home_full_time_score > m.away_full_time_score
    THEN 1 else 0 end)