MySql为体育联赛划分.pct
出于某种原因,我无法写出一段简单的代码 我想在下面的代码中将“赢”除以“玩”,这样在我的输出中,如果你赢了4场比赛中的2场,那么它显示为.500 当然,这很简单,一个位置可能是关键,但我无法理解它为我的生活MySql为体育联赛划分.pct,mysql,Mysql,出于某种原因,我无法写出一段简单的代码 我想在下面的代码中将“赢”除以“玩”,这样在我的输出中,如果你赢了4场比赛中的2场,那么它显示为.500 当然,这很简单,一个位置可能是关键,但我无法理解它为我的生活 $result = mysql_query(" select team, count(*) played, count(case when HomeScore > AwayScore then 1 end) wins, count(case whe
$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的行为,将布尔表达式自动转换为int。因此,这意味着不需要CASE语句,也不需要以牺牲可移植性为代价的查询。此外,像SUM3*win+draw score这样的部件在视觉上看起来更像您的分数计算公式。计算pct没有问题
我现在得到警告:mysql_fetch_assoc期望参数1是resource,布尔值在第41行的/home/peterborough/www/www/leaguetable.php中给出,这是我的while stmt,它以前工作过:while$row=mysql_fetch_assoc$result{echo;echo.$row['team'];echo.$row['wins'];echo.$row['lost'];echo.$row['percentage'];echo;}echo;这看起来更像是这样,虽然对于一支赢了4/5场比赛的球队来说,它显示为.4000,而它应该是.800。我们需要找出如何使它成为小数点后3位,而不是4毫米。在我的测试中,我有一支赢了4/5场比赛的球队,并且正确显示为0.8000。我不知道它怎么会变成0.4000。我只是把模式放在SQL FIDLE中。。。至于十进制精度,这实际上更多的是在表示/客户端。您可以通过应用程序控制要显示的位置,例如:PHP。
SELECT team
, COUNT(*) played
, SUM(win) wins
, SUM(loss) lost
, SUM(win)/count(*) pctWon
, SUM(draw) draws
, SUM(SelfScore) ptsfor
, SUM(OpponentScore) ptsagainst
, SUM(SelfScore) - SUM(OpponentScore) goal_diff
, SUM(3*win + draw) score
FROM (
SELECT team
, SelfScore
, OpponentScore
, SelfScore < OpponentScore win
, SelfScore > OpponentScore loss
, SelfScore = OpponentScore draw
FROM (
SELECT HomeTeam team, HomeScore SelfScore, AwayScore OpponentScore
FROM Game
union all select AwayTeam, AwayScore, HomeScore
FROM Game
) a
) b
GROUP BY team
ORDER BY wins DESC, draws DESC, lost ASC, goal_diff DESC;