php / mysql - расчеты по результатам матчей (цели) - PullRequest
0 голосов
/ 25 сентября 2018

У меня есть база данных со всеми футбольными матчами за сезон, и я хочу сделать несколько простых расчетов с результатами.Вот как выглядит таблица:

+----------+----------+---------------+---------------+--+
| 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 |
+-------+----------+--------------+----------+--------------+

Ответы [ 2 ]

0 голосов
/ 25 сентября 2018

Вы можете попробовать это

вход:

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);

enter image description here

Выход:

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 ;

enter image description here

0 голосов
/ 25 сентября 2018

Попробуйте это с объединением всех:

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
...