我有一個表的「遊戲」,看起來像這樣: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不知道它的正確的路要走。
這個問題肯定已經問和回答過。 – Strawberry
是否可以在數據庫中存儲額外的信息?因爲它似乎是最簡單的解決方案,就是爲每場比賽存儲主隊的積分(3,1或0),使得選擇更快更簡單。 – Ynhockey
參見http://www.artfulsoftware.com/infotree/qrytip.php?id=804 – Strawberry