我需要以下查询的帮助,我将发布该表的示例。
player | goals | gamedate
playername1 | 1 | 2011-01-01
playername2 | 2 | 2011-01-01
playername1 | 1 | 2011-01-02
playername3 | 1 | 2011-01-01
playername1 | 2 | 2011-01-03
playername1 | 1 | 2011-01-01
playername3 | 1 | 2011-01-01
playername2 | 2 | 2011-01-01
playername1 | 1 | 2011-01-04
还有更多的数据,我想知道如何编写一个查询,在一段时间(游戏季节)之间为顶级玩家提供最多的目标?
编辑:似乎我对数据结构的方式有误。对不起,我很抱歉。我将离开上一张桌子,以便每个人都可以看到第一个答案。我不想打开一个新问题,因为它基本上是一回事。我的道歉Haim。
`table1`
id | gamedate
1 | 2011-01-01
2 | 2011-01-02
3 | 2011-01-03
4 | 2011-01-04
5 | 2011-01-05
`table2`
id | gameid | player | goals
1 | 1 | playername1 | 1
2 | 1 | playername2 | 2
3 | 2 | playername1 | 1
4 | 1 | playername3 | 1
5 | 3 | playername1 | 2
6 | 4 | playername1 | 1
7 | 2 | playername3 | 1
8 | 3 | playername2 | 2
9 | 5 | playername1 | 1
gameid
是由table1
中的ID引用的外键。
答案 0 :(得分:1)
替换您要检查的日期, 并将限制(在示例中为5)设置为您想要的玩家数量。
SELECT player , sum(goals) AS top_scores FROM mytable
WHERE gamedate BETWEEN '2011-01-02' AND '2011-01-07'
GROUP BY player ORDER BY top_scores DESC LIMIT 5
编辑(根据问题编辑,你只需要添加一个连接):
SELECT player , sum(goals) AS top_scores FROM table2 t2
LEFT JOIN table1 t1 ON t1.id = t2.gameid
WHERE gamedate BETWEEN '2011-01-02' AND '2011-01-07'
GROUP BY player ORDER BY top_scores DESC LIMIT 5