2015-11-04 96 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