MySQL连接三个表并求平均值
我一直在困惑如何构造一个特殊的查询,我能想到的最好的办法是它必须是某种复杂的子查询系统。
不过我并不完全确定。
我有三个表:teams
、matches
和 scored
。
teams
表包含团队编号和相应团队名称的列表。
matches
表包含记录的比赛结果列表(每场比赛每队一行)以及相应的球队编号。
scored
表包含有关球队每次得分的所有信息以及相应的比赛结果 ID 的列表。
┌────────────┐
| TEAMS |
├────────────┤
│ teamnumber |
│ teamname |
└────────────┘
┌───────────────┐
| MATCHES |
├───────────────┤
│ teamnumber |
│ matchresultid |
└───────────────┘
┌───────────────┐
| SCORED |
├───────────────┤
│ matchscoredid |
│ matchresultid |
└───────────────┘
给定一个团队号码,我需要获取每个 matchresultid
的 scored
的平均行数。我该怎么做呢?
I've been puzzling over how to structure a special query, and the best I can figure out is that it would have to be some kind of complex subquery system.
I'm not entirely sure, though.
I have three tables, teams
, matches
, and scored
.
The teams
table contains a list of team numbers and corresponding team names.
The matches
table contains a list recorded match results (one row per team per match), with the corresponding team number.
The scored
table contains a list of all of the information about each score the team made, and the corresponding match result id.
┌────────────┐
| TEAMS |
├────────────┤
│ teamnumber |
│ teamname |
└────────────┘
┌───────────────┐
| MATCHES |
├───────────────┤
│ teamnumber |
│ matchresultid |
└───────────────┘
┌───────────────┐
| SCORED |
├───────────────┤
│ matchscoredid |
│ matchresultid |
└───────────────┘
Given a teamnumber, I need to get the average number of rows in scored
per matchresultid
. How would I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你遗漏的一个可能让其他人感到困惑的元素是特定球队所对阵的比赛的实际得分。所以基本上你想要一支球队,并且在整个赛季中,你想要他们获得的平均 # 分...
如果你想要比较所有球队的平均分,请忽略 WHERE 子句并按以下方式进行分组...
如果得分表每场比赛有多行,那么每场比赛需要进行预聚合,首先获得总分,然后获得平均值...类似于
在这个版本的查询中,如果您想比较所有球队,删除特定团队的内部 WHERE 子句,并将 group by 应用到按 teamnumber 和 teamname 进行的 OUTER 查询。
为了获得整个列表的额外限定符,我已更改为 LEFT 连接,并以 IFNULL() 表示平均值...
I think the one element you left out that may have confused others is the actual score of the game a given team was playing against. So basically you want one team, and through the course of the season, you want the average # points they scored...
If you want a comparison of ALL teams averages, ignore the WHERE clause and just do a group by...
If the scored table has multiple rows per game, then a pre-aggregate would need to be done per game to have total points first, THEN get the average... something like
In THIS version of the query, if you wanted to compare all teams, remove the inner WHERE clause for a specific team, and apply the group by to the OUTER query by teamnumber and teamname.
To get your extra qualifier for the entire list, I've changed to LEFT joins, and finished with an IFNULL() for the average...
您需要分数数量还是平均分数?如果需要平均分,则需要将“count”替换为“avg”
Do you need the number of scores or the score average? If you need the score average, you need to replace the "count" with "avg"
试试这个:
Try this:
这应该返回每支球队每场比赛的得分数。
That should return the number of scores for each team per match.