表中每组底部 N 行的列的平均值
我想获得最后 5 行的列的平均值(如果玩家的行数少于 5 行(游戏),那么它应该不返回任何内容..)
如果我可以使用子查询,那么它会很简单,但是因为这不受支持,所以我不知道如何处理它。
下面的 sql 是我到目前为止所拥有的,如果玩家玩了 5 场以上的游戏,这将得到每个玩家所有游戏的平均值。
CREATE VIEW last_5_avg
AS
SELECT player, avg(score) FROM game_stats
JOIN games
ON games.id = games_stats.id GROUP BY player HAVING COUNT(games.id) > 4
ORDER BY games.id DESC;
看起来解决这个问题的唯一方法是为每个玩家的最后 5 个 game_stats 创建一个视图,然后使用第二个视图来计算平均值。
I want to get the average of a column for the last 5 rows (if there are less than 5 rows (games) for a player then it should return nothing..)
If I could use a subquery then it would be straight forward, but since this isn't supported Im not sure how to approach it.
The sql below is what I have so far, this will get me the average for all games for each player if the player has played more than 5 games.
CREATE VIEW last_5_avg
AS
SELECT player, avg(score) FROM game_stats
JOIN games
ON games.id = games_stats.id GROUP BY player HAVING COUNT(games.id) > 4
ORDER BY games.id DESC;
Looks like the only way around this problem is to create a view for the last 5 game_stats of each player and then use a second view to do the averages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用限制。
确保您的条件按以下顺序排列 -
参考:http://dev.mysql.com/doc/refman/5.1/en/select.html
Use LIMIT.
Make sure that you have your conditions in the following order -
Reference: http://dev.mysql.com/doc/refman/5.1/en/select.html
http://dev.mysql.com/doc/refman/5.0/en /select.html
我认为
LIMIT
就是你想要的。http://dev.mysql.com/doc/refman/5.0/en/select.html
I think
LIMIT
is what you want.