我有一个数据库,里面有一个赛季所有的足球比赛,我想对结果做一些简单的计算。这张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 |
+-------+----------+--------------+----------+--------------+
2条答案
按热度按时间ikfrs5lh1#
请尝试以下方法:
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=1a1bbfadd62ed68808605a9e7767870d
输出:
klh5stk12#
你可以试试这个
输入:
输出: