2016-11-14 55 views
1

我正在製作一個小型足球預測網站,我需要的一項功能是從包含過去結果的數據庫生成排名表。我的數據庫有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.Do工會通過點的數量,而不是勝

的數量產生的總體排名表

2.Rank隊

+0

那麼你爲每場比賽輸入2行?我認爲你需要重新考慮數據模型。 –

回答

1

您的解決方案是不是遠離查詢,我會用。我會在一個子查詢在外部查詢相結合的2個查詢,而不group by子句並做組由相加勝,負,平,並指出:

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抽籤。