gpt4 book ai didi

php - 梦幻足球: Compare a team's average points to the league average?

转载 作者:行者123 更新时间:2023-11-28 23:21:52 25 4
gpt4 key购买 nike

我在 MySQL 表中有梦幻足球联赛的数据。我想查询一个 php 页面的数据。

我如何创建一个查询来计算球队得分与该特定年份联盟平均得分的比较?

我的(简化的)数据表如下所示:

游戏 table :

year | team | Week | Points
2016 | Wildcats | 1 | 44
2016 | Wildcats | 2 | 52
2015 | Hawks | 1 | 36
2015 | Hawks | 2 | 58
2016 | Hawks | 1 | 43

我如何找出野猫队 2016 年的平均得分 (48.0) 与 2016 年所有球队的平均得分 (46.3) 之间的差异?

我可以使用此查询计算这两个数字,但不确定如何进行减法。

SELECT g1.year, g1.team, AVG(g1.points) as ppg,
(SELECT AVG(points)
FROM game AS g2
WHERE g2.year=g1.year) AS seasAvg
FROM game AS g1
GROUP BY g1.team, g1.year
ORDER BY ppg

最佳答案

将整个事情包装在一个外部查询中,然后您可以计算出差异,同时保持简短。

SELECT a.*, ppg - seasAvg AS diff FROM (
SELECT g1.year, g1.team, AVG(g1.points) as ppg,
(SELECT AVG(points)
FROM game AS g2
WHERE g2.year=g1.year) AS seasAvg
FROM game AS g1
GROUP BY g1.team, g1.year
ORDER BY ppg
) AS a

关于php - 梦幻足球: Compare a team's average points to the league average?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41196918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com