进球得分(范围)

时间:2013-12-31 00:05:44

标签: mysql sql

我正在尝试创建一个mysql查询,该查询将显示团队在比赛期间进球的时间。我有一个像这样设置的表:clubid(int)和minute(tinyint)。这就是我想要实现的目标:

分1-15个31球

分16-30个43球

分31-45 36球

分钟46-60 51个进球

分钟61-75 48球

分76-90 52球

首先,我想为联赛中的每支球队做到这一点,然后为每支球队分开。这是我对整个联盟的查询尝试,但不幸的是它不起作用。

  SELECT *,count(minute) as summary 
  CASE WHEN minute >= 1  AND minute <= 15 THEN  '1-15'
       WHEN minute >= 16 AND minute <= 30 THEN '16-30' 
       WHEN minute >= 31 AND minute <= 45 THEN '31-45' 
       WHEN minute >= 46 AND minute <= 60 THEN '46-60' 
       WHEN minute >= 61 AND minute <= 75 THEN '61-75'
       WHEN minute >= 76 AND minute <= 90 THEN '76-90'
       WHEN minute > 90  AND THEN 90+
       ELSE 'no goals' END as range          
  FROM teamgoals 
  ORDER BY range asc

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

请尝试sqlFiddle

SELECT IFNULL(T1.summary,0) as summary,
       rangeDescription.`range`
  FROM (      SELECT '1-15' as `range`, 1 as rowOrder
        UNION SELECT '16-30'        , 2
        UNION SELECT '31-45'        , 3
        UNION SELECT '46-60'        , 4
        UNION SELECT '61-75'        , 5
        UNION SELECT '76-90'        , 6
        UNION SELECT '90+'          , 7
        )rangeDescription
  LEFT JOIN
        (SELECT count(minute) as summary, 
          CASE WHEN minute >= 1  AND minute <= 15 THEN  '1-15'
               WHEN minute >= 16 AND minute <= 30 THEN '16-30' 
               WHEN minute >= 31 AND minute <= 45 THEN '31-45' 
               WHEN minute >= 46 AND minute <= 60 THEN '46-60' 
               WHEN minute >= 61 AND minute <= 75 THEN '61-75'
               WHEN minute >= 76 AND minute <= 90 THEN '76-90'
               WHEN minute > 90   THEN '90+'
               ELSE 'no goals' 
          END as `range` 
        FROM
        teamgoals
        WHERE clubId = 1
         GROUP BY `range`
        )T1
   ON rangeDescription.`range` = T1.`range`
ORDER BY rangeDescription.rowOrder asc