MySQL连接三个表并求平均值

发布于 2024-10-17 04:13:39 字数 742 浏览 2 评论 0原文

我一直在困惑如何构造一个特殊的查询,我能想到的最好的办法是它必须是某种复杂的子查询系统。

不过我并不完全确定。

我有三个表:teamsmatchesscored

teams 表包含团队编号和相应团队名称的列表。

matches 表包含记录的比赛结果列表(每场比赛每队一行)以及相应的球队编号。

scored 表包含有关球队每次得分的所有信息以及相应的比赛结果 ID 的列表。

┌────────────┐
| TEAMS      |
├────────────┤
│ teamnumber |
│ teamname   |
└────────────┘

┌───────────────┐
| MATCHES       |
├───────────────┤
│ teamnumber    |
│ matchresultid |
└───────────────┘

┌───────────────┐
| SCORED        |
├───────────────┤
│ matchscoredid |
│ matchresultid |
└───────────────┘

给定一个团队号码,我需要获取每个 matchresultidscored 的平均行数。我该怎么做呢?

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 技术交流群。

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

发布评论

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

评论(4

千紇 2024-10-24 04:13:39

我认为你遗漏的一个可能让其他人感到困惑的元素是特定球队所对阵的比赛的实际得分。所以基本上你想要一支球队,并且在整个赛季中,你想要他们获得的平均 # 分...

select 
      t.teamnumber,
      t.teamname,
      avg( s.totalpoints ) TeamAvgPoints
   from 
      teams t
         join matches m
            on t.teamnumber = m.teamnumber
            join scored s
               on m.matchresultid = s.matchresultid
   where 
      t.teamnumber = SomeValue

如果你想要比较所有球队的平均分,请忽略 WHERE 子句并按以下方式进行分组...

   group by
      t.teamnumber, 
      t.teamname

如果得分表每场比赛有多行,那么每场比赛需要进行预聚合,首先获得总分,然后获得平均值...类似于

select
      PreQuery.TeamNumber,
      PreQuery.TeamName,
      avg( ifnull( PreQuery.MatchTotalPoints, 0 ) ) AvgPerGame
   from 
      ( select 
            t.teamnumber,
            t.teamname,
            m.matchresultid,
            count(*) MatchTotalPoints
         from 
            teams t
               left join matches m
                  on t.teamnumber = m.teamnumber
                  left join scored s
                     on m.matchresultid = s.matchresultid
         where 
            t.teamnumber IN( SomeValue, AnotherValue, MoreValues, EtcValue )
         group by
            t.teamnumber,
            t.teamname,
            m.matchresultid ) PreQuery
   group by 
       PreQuery.TeamNumber,
       PreQuery.TeamName

在这个版本的查询中,如果您想比较所有球队,删除特定团队的内部 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...

select 
      t.teamnumber,
      t.teamname,
      avg( s.totalpoints ) TeamAvgPoints
   from 
      teams t
         join matches m
            on t.teamnumber = m.teamnumber
            join scored s
               on m.matchresultid = s.matchresultid
   where 
      t.teamnumber = SomeValue

If you want a comparison of ALL teams averages, ignore the WHERE clause and just do a group by...

   group by
      t.teamnumber, 
      t.teamname

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

select
      PreQuery.TeamNumber,
      PreQuery.TeamName,
      avg( ifnull( PreQuery.MatchTotalPoints, 0 ) ) AvgPerGame
   from 
      ( select 
            t.teamnumber,
            t.teamname,
            m.matchresultid,
            count(*) MatchTotalPoints
         from 
            teams t
               left join matches m
                  on t.teamnumber = m.teamnumber
                  left join scored s
                     on m.matchresultid = s.matchresultid
         where 
            t.teamnumber IN( SomeValue, AnotherValue, MoreValues, EtcValue )
         group by
            t.teamnumber,
            t.teamname,
            m.matchresultid ) PreQuery
   group by 
       PreQuery.TeamNumber,
       PreQuery.TeamName

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...

围归者 2024-10-24 04:13:39

您需要分数数量还是平均分数?如果需要平均分,则需要将“count”替换为“avg”

SELECT s.matchresultid, COUNT(s.matchscoredid) 
  FROM matches m INNER JOIN scored s ON m.matchresultid = s.matchresultid
 WHERE m.teamnumber = <team>
GROUP BY s.matchresultid

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"

SELECT s.matchresultid, COUNT(s.matchscoredid) 
  FROM matches m INNER JOIN scored s ON m.matchresultid = s.matchresultid
 WHERE m.teamnumber = <team>
GROUP BY s.matchresultid
千笙结 2024-10-24 04:13:39

试试这个:

SELECT s.matchresultid, count(*) 
FROM scored s
JOIN matches m
ON m.matchresultid = s.matchresultid
WHERE m.teamnumber = your_team_number
GROUP BY s.matchresultid

Try this:

SELECT s.matchresultid, count(*) 
FROM scored s
JOIN matches m
ON m.matchresultid = s.matchresultid
WHERE m.teamnumber = your_team_number
GROUP BY s.matchresultid
逆光飞翔i 2024-10-24 04:13:39
SELECT a.teamname team, b.matchresultid match, COUNT(c.matchscoredid) scores 
FROM teams a, matches b, scored c 
WHERE a.teamnumber = b.teamnumber AND b.matchresultid = c.matchresultid;

这应该返回每支球队每场比赛的得分数。

SELECT a.teamname team, b.matchresultid match, COUNT(c.matchscoredid) scores 
FROM teams a, matches b, scored c 
WHERE a.teamnumber = b.teamnumber AND b.matchresultid = c.matchresultid;

That should return the number of scores for each team per match.

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