我有一个表的“游戏”,看起来像这样: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