我的表格如下:
Year Week Points
2007 1 20
2007 1 15
2007 2 15
2007 2 15
2008 1 8
2008 1 10
我希望我的结果是:
Most Points: 35 (2007 Week 1)
2nd Most Points: 30 (2007 Week 2)
3rd Most Points: 18 (2008 Week 1)
我正在使用:
$query = "SELECT SUM(POINTS) ";
$query .= " FROM SCHEDULE";
$query .= " AND WEEK = '1' ";
$query .= " AND YEAR = '2008'";
有没有办法让积分SUM不必为每周和每年运行单独的查询?
答案 0 :(得分:1)
您可以使用GROUP BY
,例如:
SELECT YEAR, MONTH, SUM(POINTS) FROM SCHEDULE GROUP BY WEEK,YEAR
然后,这将形成具有相同WEEK
和YEAR
的项目组,然后对这些项目进行求和。
答案 1 :(得分:0)
这听起来像一个简单的查询。你试过这个吗? :
select `Year`, `Week`, sum(`Points`) as Total_Points
from myTable
group by `Year`, `Week`
order by sum(`Points`) desc
limit 3
答案 2 :(得分:0)
您的查询应该是
SELECT
SUM(Points) total,
CONCAT(`Year`, ' Week ', `Week`)
FROM
SCHEDULE
GROUP BY
`Year`,
`Week`,
ORDER BY
total DESC