php/mysql-匹配结果计算(目标)

wfypjpf4  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(403)

我有一个数据库,里面有一个赛季所有的足球比赛,我想对结果做一些简单的计算。这张table看起来像:

+----------+----------+---------------+---------------+--+
| HomeTeam | AwayTeam | HomeTeamGoals | AwayTeamGoals |  |
+----------+----------+---------------+---------------+--+
| team1    | team2    |             3 |             1 |  |
| team3    | team4    |             1 |             2 |  |
| team1    | team3    |             4 |             4 |  |
| team4    | team2    |             0 |             1 |  |
+----------+----------+---------------+---------------+--+

现在我要为每个团队计算4件事:
-主场总进球数
-客场总进球数
-主场比赛中对阵对手的总进球数
-客场对战总进球数
通过这个查询,我正确地获得了家庭游戏的所有信息:

SELECT HomeTeam, SUM(HomeTeamGoals) as HomeTeamGoals, SUM(AwayTeamGoals) as AwayTeamGoals
FROM games
GROUP BY HomeTeam

但是我现在如何获得客场比赛的信息(来自同一个查询)?
根据上表的数据,结果如下:

+-------+----------+--------------+----------+--------------+
| team  | home for | home against | away for | away against |
+-------+----------+--------------+----------+--------------+
| team1 |        7 |            5 |        0 |            0 |
| team2 |        0 |            0 |        2 |            3 |
| team3 |        1 |            2 |        4 |            4 |
| team4 |        0 |            1 |        2 |            1 |
+-------+----------+--------------+----------+--------------+
ikfrs5lh

ikfrs5lh1#

请尝试以下方法:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1a1bbfadd62ed68808605a9e7767870d

select hometeam, sum(HomeTeamGoals) as homefor,sum(AwayTeamGoals) as homeagainst, 0 as awayfor, 0 as awayagainst from tablename
group by hometeam

union all

select AwayTeam, 0,0, sum(HomeTeamGoals) as awayfor, sum(AwayTeamGoals) as awayagainst from tablename
group by AwayTeam

输出:

hometeam    homefor homeagainst awayfor awayagainst
team1       7       5            0      0
team4       0       1            0      0
team2       0       0            3      2
team3       0       0            4      4
klh5stk1

klh5stk12#

你可以试试这个
输入:

create table goals(
homeTeam varchar(10),
awayTeam varchar(10),
homeTeamGoals tinyint,
awayTeamGoals tinyint
);
insert into goals values('team1','team2',3,1),('team3','team4',1,2),('team1','team3',4,4),('team4','team2',0,1);


输出:

select totalTeams.team as hometeam,
  case when a.totalHomeGoals IS NULL THEN 0 ELSE a.totalHomeGoals END as totalHomeGoals,
  case when a.totalHomeAgainstGoals IS NULL THEN 0 ELSE a.totalHomeAgainstGoals END as totalHomeAgainstGoals,
  case when b.totalAwayGoals IS NULL THEN 0 ELSE b.totalAwayGoals END as totalAwayGoals,
  case when b.totalAwayAgainstGoals IS NULL THEN 0 ELSE b.totalAwayAgainstGoals END as totalAwayAgainstGoals
      from (select homeTeam as team from goals union select awayTeam from goals) as totalTeams 
      left join (select homeTeam as team, sum(homeTeamGoals) as totalHomeGoals,sum(awayTeamGoals) as totalHomeAgainstGoals from goals group by hometeam) as a on totalTeams.team=a.team
      left join (select awayTeam as team,sum(awayTeamGoals) as totalAwayGoals,sum(homeTeamGoals) as totalAwayAgainstGoals from goals group by awayTeam) as b on totalTeams.team=b.team 
      order by hometeam ;

相关问题