选择并计算查询在MySQL中无法正常工作

时间:2014-05-25 03:14:51

标签: mysql sql

该表包含(id,name,place,month,year)。

每个月都有随机数量的参赛者参赛。结果列于上表中。所需的报告如下:

name, month, place, total for that month

查询已尝试:

SELECT name, month, place, COUNT( results.month ) AS Total
FROM  results
WHERE MONTH !=0
AND name = 'some name'
AND year=2014
GROUP BY month
ORDER BY MONTH 
LIMIT 0 , 30

示例输出

some name, 5, 13, 1

期望的输出

some name, 5, 13, 25 (or whatever the number of persons was for a particular month)

我理解为什么我的小组每个月都没有给我预期的结果。我只是在努力理解我需要做些什么来获得预期的结果。

1 个答案:

答案 0 :(得分:0)

如果您想要一个名字加上所有参赛者的数量,那么您可以使用子查询。目前还不清楚你是否想要一年中的所有参赛者:

SELECT month, name, place,
       (select COUNT(*) from results where month <> 0 and year = 2014) AS Total
FROM  results
WHERE MONTH <> 0 AND 
      name = 'some name'
      year = 2014
ORDER BY MONTH 
LIMIT 0 , 30;

或者月份:

SELECT month, name, place,
       (select COUNT(*) from results r2 where r2.month = r.month and r2.year = r.year
       ) AS Total
FROM  results r
WHERE MONTH <> 0 AND 
      name = 'some name'
      year = 2014
ORDER BY MONTH 
LIMIT 0 , 30;