MySQL分组问题

发布于 2024-11-30 16:07:50 字数 1426 浏览 1 评论 0原文

我有一个运动追踪器 MySQL 数据库,正在尝试提取一些统计数据;相关表格概述如下:

游戏

id bigint(20) unsigned PK
round_id bigint(20)
team_1_id bigint(20) 无符号
team_2_id bigint(20) 无符号
日期 日期时间

团队

ID bigint(20) 未签名 PK
name varchar(128)

玩家(此查询并非真正需要)

id bigint(20) unsigned PK
名字 varchar(128)
姓氏 varchar(128)
dob datetime

player_team_game

id bigint(20) 未签名 PK
game_id bigint(20) 无符号
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,因此分组需​​要在单独的记录集中反映每个团队的结果。

I have a sports tracker MySQL database and am trying to extract some stats; relevant tables are outlined below:

game

id bigint(20) unsigned PK
round_id bigint(20)
team_1_id bigint(20) unsigned
team_2_id bigint(20) unsigned
date datetime

team

id bigint(20) unsigned PK
name varchar(128)

player (not really required for this query)

id bigint(20) unsigned PK
first_name varchar(128)
surname varchar(128)
dob datetime

player_team_game

id bigint(20) unsigned PK
game_id bigint(20) unsigned
player_id bigint(20) unsigned
team_id bigint(20)

stats

id bigint(20) PK
player_team_game_id bigint(20)
kicks int(11)
goals int(11)
tackles int(11)

The intent is to determine total kicks, goals and tackles for each team in each game. An example resultset might look like the following:

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  

Can anyone help me with an appropriate query?

Edit: potentially I haven't made the problem clear; the issue is that the game table has both game.team_id_1 and game.team_id_2 so grouping needs to reflect the results for each team in separate recordsets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断爱 2024-12-07 16:07:50

试试这个:

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

Try this:

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
烏雲後面有陽光 2024-12-07 16:07:50

我认为下面应该给出您想要的结果。

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 

I would think that the below should give the result that you want.

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文