gpt4 book ai didi

MySql Divide for .pct for sports league

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

出于某种原因,我无法推测出一段简单的代码。

我想在下面的代码中将 'wins' 除以 'played',这样在我的输出中,如果你在 4 场比赛中赢了 2 场,它会显示为 .500?

当然很简单,位置可能是关键,但我终究无法理解!!

$result = mysql_query("
select team,
count(*) played,
count(case when HomeScore > AwayScore then 1 end) wins,
count(case when AwayScore > HomeScore then 1 end) lost,
count(case when HomeScore = AwayScore then 1 end) draws,
sum(HomeScore) ptsfor,
sum(AwayScore) ptsagainst,
sum(HomeScore) - sum(AwayScore) goal_diff,
sum(case when HomeScore > AwayScore then 3 else 0 end + case
when HomeScore = AwayScore then 1 else 0 end) score
from (select
HomeTeam team,
HomeScore,
AwayScore
from Game
union all
select AwayTeam,
AwayScore,
HomeScore
from Game) a
group by team
order by wins desc,
draws desc,
lost asc,
goal_diff desc;");

谢谢

最佳答案

只需将另一个计算列添加到查询中,如下所示:

select team, 
count(*) played,
count(case when HomeScore > AwayScore then 1 end) wins,
(cast(count(case when HomeScore > AwayScore then 1 end) as decimal(8,2)/cast(count(*) as decimal(8,2)) percentage,
.... -> Rest of the query goes here

转换已经完成,因为我假设分数将是整数。如果不进行转换,精度将会下降,即 2/4 将在不进行转换的情况下返回 0。

关于MySql Divide for .pct for sports league,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24147517/

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