MySQL:如何总结球员得分的所有目标

时间:2019-02-27 13:24:14

标签: mysql mysqli

我想问一下我如何总结过去7天内所有球员得分的进球。
该表的名称为 player_games ,其中包含玩游戏日期的字段为 game_played。

我尝试对分数进行求和,但分数结果为0。上个星期不包括在内。

default_socket_timeout

4 个答案:

答案 0 :(得分:1)

您可以使用DATE_SUBCURDATE使用以下内容:

SELECT player_id AS playerID, SUM(scores) AS scores 
FROM player_games
WHERE game_played BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()
GROUP BY player_id 
ORDER BY SUM(scores) DESC

您还可以直接在WHERE条件下使用日期计算,而无需使用DATE_SUB

SELECT player_id AS playerID, SUM(scores) AS scores 
FROM player_games
WHERE game_played BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE()
GROUP BY player_id 
ORDER BY SUM(scores) DESC

答案 1 :(得分:0)

您可能正在寻找时间间隔:

SELECT SUM(goals) FROM PLAYER_GAMES WHERE GAME_PLAYED >= CURDATE() - INTERVAL 7 DAY;

编辑:

不要对字符串求和(即,不要使用引号)。

答案 2 :(得分:0)

str(f).encode('utf-8')

答案 3 :(得分:-1)

以下应该有效 SELECT c.name, SUM(g.goals) FROM 国家为 c RIGHT JOIN 目标为 g ON c.id = g.country_id GROUP BY c.id, c.name 按 c.name ASC 排序