gpt4 book ai didi

来自两个表的MYSQL QUERY总和

转载 作者:行者123 更新时间:2023-11-30 23:35:13 25 4
gpt4 key购买 nike

嘿,我在使用以下 Mysql 查询时遇到了一些麻烦,我如何从 team_stats 表而不是 driver_stats 表中计算 SUMS(这是计算点,查询中的第 5 和第 6 总和)与现在 team_id = 3 的条件相同。我所需要的只是将这些计算从 driver_stats 表交换到 team_stats 表,我遇到的问题是当我尝试这样做时,计算量太高了不只是为了 where team_id = '3' 或任何数字。

如果有任何帮助,我们将不胜感激。

SELECT       t.teamname,
t.value,
SUM(IF(qual_pos = '1', 1,0)) AS poles,
SUM(IF(race_pos <= '3', 1,0)) AS podiums,
SUM(IF(race_pos = '1', 1,0)) AS victories,
SUM(overtakes) AS overtakes,
SUM(CASE
WHEN s.track_id = (SELECT MAX(track_id) FROM driver_stats) THEN
points
ELSE
0
End) AS lastracepoints,

# CHANGE THIS SUM AND THE SUM ABOVE TO CALCULATE
# the points column in the tale named TEAM_STATS
# but with the same restrictions as now,
# only where team_id = 3
SUM(points) AS points

FROM driver_stats s
LEFT JOIN drivers d
ON d.drivers_id = s.drivers_id
LEFT JOIN teams t
ON d.team_id = t.team_id
WHERE d.team_id = 3

最佳答案

在特殊情况下,MySQL 可以让您在不定义 group by 子句的情况下使用聚合(sum),但这不是典型的,对您不起作用。

Is it possible to use Aggregate function in a Select statment without using Group By clause?

我还没有通过 try 测试过这个:

SELECT       t.teamname,
t.value,
SUM(IF(qual_pos = '1', 1,0)) AS poles,
SUM(IF(race_pos <= '3', 1,0)) AS podiums,
SUM(IF(race_pos = '1', 1,0)) AS victories,
SUM(overtakes) AS overtakes,
SUM(CASE
WHEN s.track_id = (SELECT MAX(track_id) FROM driver_stats) THEN
points
ELSE
0
End) AS lastracepoints,
SUM(points) AS points

FROM drivers d
INNER JOIN driver_stats s
ON d.drivers_id = s.drivers_id
AND d.team_id = 3
LEFT JOIN teams t
ON d.team_id = t.team_id
GROUP BY
teamname,
`value`

关于来自两个表的MYSQL QUERY总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8027686/

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