一个查询中的MySQL足球统计数据

时间:2015-11-04 07:49:35

标签: 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

最好的方法是在一个查询中获取所有这些内容。我担心这可能很难。所以我可以想象我需要有多个查询。 目前我有2个查询只是为了在家里玩的团队获取数据:

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相加以获得总数。 有没有办法只执行一次此查询?我尝试过工会,但我不确定它是否正确。

1 个答案:

答案 0 :(得分: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'

Ant使用此长查询以您指定的确切格式获取结果:

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'