我有一个运动追踪器MySQL数据库,我正试图提取一些统计数据;相关表格概述如下:
id bigint(20)unsigned PK
round_id bigint(20)
team_1_id bigint(20)未签名的
team_2_id bigint(20)unsigned
日期日期时间
id bigint(20)unsigned PK
name varchar(128)
id bigint(20)unsigned PK
first_name varchar(128)
姓氏varchar(128)
dob datetime
id bigint(20)unsigned PK
game_id bigint(20)unsigned
player_id bigint(20)无符号
team_id bigint(20)
id bigint(20)PK
player_team_game_id bigint(20)
踢int(11)
目标int(11)
解决int(11)
目的是确定每场比赛中每支球队的总踢球,进球和铲球。示例结果集可能如下所示:
game.id | game.date | team.name | stats.kicks | stats.goals | stats.tackles
1 | 2011-01-01 | team1 | 25 | 30 | 35
1 | 2011-01-01 | team2 | 26 | 31 | 36
2 | 2011-01-01 | team3 | 27 | 32 | 37
2 | 2011-01-01 | team4 | 28 | 33 | 38
3 | 2011-01-02 | team5 | 29 | 34 | 39
3 | 2011-01-02 | team6 | 30 | 35 | 40
任何人都可以帮我找到合适的查询吗?
编辑:可能我没有明确问题;问题是游戏桌有game.team_id_1和game.team_id_2所以分组需要反映每个团队在不同记录集中的结果。
答案 0 :(得分:1)
试试这个:
SELECT
g.id, g.date,
t.name,
SUM(s.kicks),
SUM(s.goals),
SUM(s.tackles)
FROM stats s
LEFT JOIN player_team_game ptg ON ptg.id = s.player_team_game_id
LEFT JOIN game g ON g.id = ptg.game_id
LEFT JOIN team t ON t.id = ptg.team_id
GROUP BY ptg.team_id
ORDER BY g.id, t.id
答案 1 :(得分:0)
我认为下面应该给出你想要的结果。
select game.id, game.date, team.name,
sum (stats.kicks), sum (stats.goals), sum (stats.tackles)
from game, player_team_game, stats, team
where player_team_game.game_id = game.id
and player_team.game.team_id = team.id
and player_team_game.id = stats.player_team_game_id
group by game.id, game.date, team.name