我正在制作一个小型足球预测网站,我需要的一项功能是从我的数据库中生成包含过去结果的联赛表。我的数据库有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.按分数而不是获胜次数对队伍进行评分
答案 0 :(得分: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为抽奖。