生成整体排名表
Posted
技术标签:
【中文标题】生成整体排名表【英文标题】:Generate an overall league table 【发布时间】:2016-11-14 21:49:17 【问题描述】:我正在制作一个小型足球预测网站,我需要的一个功能是从我的数据库中生成一个包含过去结果的联赛表。我的数据库有 5 列;
Fixture_ID。
Home_team。
Away_team。
Htgoals(主队进球数)。
Atgoals(客队进球数)。
我有两个查询只在主场和客场比赛时返回球队的联赛表
Home-" Select results.Home_team,
SUM(if(results.Htgoals > results.Atgoals,3,0)) AS W,
SUM(IF(results.Htgoals = results.Atgoals,1,0)) AS D,
SUM(IF(results.Htgoals < results.Atgoals,1,0)) AS L
from results
GROUP BY results.Home_team
order by W desc";
Away table-" Select results.Away_team,
SUM(if(results.Atgoals > results.Htgoals,3,0)) AS W,
SUM(IF(results.Atgoals = results.Htgoals,1,0)) AS D,
SUM(IF(results.Atgoals < results.Htgoals,1,0)) AS L
FROM results
GROUP BY results.Away_team
ORDER BY W DESC";
我该怎么做;
1.做一个联合生成一个整体排名表
2.根据积分而不是获胜次数对球队进行排名
【问题讨论】:
所以你为每个匹配输入 2 行?我认为您需要重新考虑数据模型。 【参考方案1】:您的解决方案与我将使用的查询相距不远。我会在一个子查询中组合没有 group by 子句的 2 个查询,并在外部查询中通过汇总胜利、失败、平局和点数来进行分组:
select team, sum(W) as W, sum(D) as D, sum(L) as L, sum(W * 3 + D) as Points
FROM
((Select results.Home_team as team,
if(results.Htgoals > results.Atgoals,1,0) AS W,
IF(results.Htgoals = results.Atgoals,1,0) AS D,
IF(results.Htgoals < results.Atgoals,1,0) AS L
from results)
UNION
(Select results.Away_team,
if(results.Atgoals > results.Htgoals,3,0) AS W,
IF(results.Atgoals = results.Htgoals,1,0) AS D,
IF(results.Atgoals < results.Htgoals,1,0) AS L
FROM results)) t
GROUP BY team
ORDER BY Points desc
在计算分数时,我假设获胜为 3 分,平局为 1 分。
【讨论】:
以上是关于生成整体排名表的主要内容,如果未能解决你的问题,请参考以下文章
R语言dplyr包使用arrange函数group_by函数mutate函数生成分组数据的排名(rank)实战(Rank Variable by Group):升序排名降序排名以及相同排名的处理