2015-11-04 91 views
0

我有一個表的「遊戲」,看起來像這樣:MySQL的足球統計

桌上游戲:

id (int) 
home_team (varchar) 
away_team (varchar) 
home_goals (int) 
away_goals (int) 

對於一個特定的比賽我需要獲取以下內容:

主隊:

home wins 
home tie 
home loss 
home scored 
home conceded 

正如我也需要了解以上所有實體總數(勝,損失,得分等),然後我基本上需要與上面相同,但在球隊扮演了,所以:

away wins 
away tie 
away loss 
away scored 
away conceded 

對於客隊:

away wins 
away tie 
away loss 
away scored 
away conceded 

,因爲我需要也知道以上所有實體總數(勝損失,得分等),然後我基本上需要與上面相同,但是當球隊發揮在家裏,所以:

home wins 
home tie 
home loss 
home scored 
home conceded 

最好是讓這一切一個查詢。我擔心這可能會很困難。所以我可以想象我需要有multipla查詢。 目前我有兩個疑問只是爲了獲得數據,播放在主場球隊:

select 
count(case when home_goals > away_goals then 1 else null end) as state_win 
,count(case when home_goals < away_goals then 1 else null end) as state_loss 
,count(case when home_goals = away_goals then 1 else null end) as state_tie 
,sum(home_goals) as state_scored 
,sum(away_goals) as state_conceded 
from 
game 
where 
home_team = 'chelsea' 

select 
count(case when home_goals < away_goals then 1 else null end) as other_win 
,count(case when home_goals > away_goals then 1 else null end) as other_loss 
,count(case when home_goals = away_goals then 1 else null end) as other_tie 
,sum(away_goals) as other_scored 
,sum(home_goals) as other_conceded 
from 
game 
where 
away_team = 'chelsea' 

我的計劃是從查詢1查詢2,以獲得總總結統計。 有沒有辦法只做一次這個查詢?我試圖與工會,但IM不知道它的正確的路要走。

+1

這個問題肯定已經問和回答過。 – Strawberry

+0

是否可以在數據庫中存儲額外的信息?因爲它似乎是最簡單的解決方案,就是爲每場比賽存儲主隊的積分(3,1或0),使得選擇更快更簡單。 – Ynhockey

+1

參見http://www.artfulsoftware.com/infotree/qrytip.php?id=804 – Strawberry

回答

1

您可以嘗試聯合將主客場比賽統計結合在一個查詢中。由於列的名稱/語義不同,簡單的聯合將不起作用。

select 
'home' as place 
,count(case when home_goals > away_goals then 1 else null end) as win 
,count(case when home_goals < away_goals then 1 else null end) as loss 
,count(case when home_goals = away_goals then 1 else null end) as tie 
,sum(home_goals) as scored 
,sum(away_goals) as conceded 
from 
game 
where 
home_team = 'chelsea' 
union 
select 
'away' as place 
,count(case when home_goals < away_goals then 1 else null end) as win 
,count(case when home_goals > away_goals then 1 else null end) as loss 
,count(case when home_goals = away_goals then 1 else null end) as tie 
,sum(away_goals) as scored 
,sum(home_goals) as conceded 
from 
game 
where 
away_team = 'chelsea' 

螞蟻利用這種長的查詢得到的結果在您指定的確切格式:

select 
a.win as state_win 
,a.loss as state_loss 
,a.tie as state_tie 
,a.scored as state_scored 
,a.conceded as state_conceded 
,h.win as other_win 
,h.loss as other_loss 
,h.tie as other_tie 
,h.scored as other_scored 
,h.conceded as other_conceded 
from 
(select 
'home' as place 
,count(case when home_goals > away_goals then 1 else null end) as win 
,count(case when home_goals < away_goals then 1 else null end) as loss 
,count(case when home_goals = away_goals then 1 else null end) as tie 
,sum(home_goals) as scored 
,sum(away_goals) as conceded 
from 
game 
where 
home_team = 'chelsea' 
union 
select 
'away' as place 
,count(case when home_goals < away_goals then 1 else null end) as win 
,count(case when home_goals > away_goals then 1 else null end) as loss 
,count(case when home_goals = away_goals then 1 else null end) as tie 
,sum(away_goals) as scored 
,sum(home_goals) as conceded 
from 
game 
where 
away_team = 'chelsea') h, 
(select 
'home' as place 
,count(case when home_goals > away_goals then 1 else null end) as win 
,count(case when home_goals < away_goals then 1 else null end) as loss 
,count(case when home_goals = away_goals then 1 else null end) as tie 
,sum(home_goals) as scored 
,sum(away_goals) as conceded 
from 
game 
where 
home_team = 'chelsea' 
union 
select 
'away' as place 
,count(case when home_goals < away_goals then 1 else null end) as win 
,count(case when home_goals > away_goals then 1 else null end) as loss 
,count(case when home_goals = away_goals then 1 else null end) as tie 
,sum(away_goals) as scored 
,sum(home_goals) as conceded 
from 
game 
where 
away_team = 'chelsea') a 
where h.place='home' and a.place='away' 
+0

它的工作原理。謝謝。但是,結果是相反的。我試圖調試。舉例來說,切爾西在主場和客場拿下了0分(但實際上是在主場1勝0平勝負)。 – user1963937

+1

噢,對不起。在最後一行切換a.place和h.place。 – vacsora