我有一个比赛表,其中包含足球(足球)部门的所有历史比赛。大约有30000场比赛,我的挑战是为特定球队和其他球队找回胜利,平局和输球。
对于我的问题,我只是使用绘图,为了检索我的绘图我有两种可能的场景:
该团队是主队
该团队是客队
所以根据我的数据,我的团队是主队的第一个场景的查询是这样的:
SELECT
matches.away_team as VS,
COUNT(matches.idmatch) AS TIES
FROM
my_football_database.matches
WHERE
home_team = 14
AND home_team_goals = away_team_goals
AND aet = "N" AND PK = "N"
GROUP BY
away_team
LIMIT 5;
输出:
+------+------+
| VS | TIES |
+------+------+
| 2 | 1 |
| 3 | 3 |
| 4 | 2 |
| 8 | 1 |
| 9 | 3 |
+------+------+
第二种情况是我的团队是客队,所以它是这样的:
SELECT
matches.home_team as VS,
COUNT(matches.idmatch) AS TIES
FROM
my_football_database.matches
WHERE
away_team = 14
AND home_team_goals = away_team_goals
AND aet = "N" AND PK = "N"
GROUP BY
home_team
LIMIT 5;
输出:
+------+------+
| VS | TIES |
+------+------+
| 2 | 4 |
| 3 | 2 |
| 7 | 1 |
| 8 | 3 |
| 9 | 1 |
+------+------+
正如您所见:
如何根据“VS”ID来总结TIES的结果?比如,在列出作为主队的球队的所有领带之后,由客队分组并列出球队作为客队进行比赛的所有领带,由主队分组,根据“Vs”ID加入两个列。
这种情况下的示例结果是:
+------+------+
| VS | TIES |
+------+------+
| 2 | 5 |
| 3 | 5 |
| 4 | 2 |
| 7 | 1 |
| 8 | 4 |
| 9 | 4 |
+------+------+
注意:我必须指出一个单一的查询,可以给我一张表格,其中列出了给定团队的所有胜利,失败和平局,并按照他们对战的球队进行分组。
这是总共14个场景。 Home and Away Draws(2),Home and Away赢得常规,奖励和惩罚(6)。 Home and Away失去正常,奖励和处罚(6)。
因此,我不是在寻找可以解决Draws交易的代码,而是更像是关于该问题的研究,研究或实施的HINT。我的第一个想法是分别创建每个场景,然后将它们用作子查询,但我被告知这对性能不方便。
答案 0 :(得分:0)
使用两个查询中的UNION
,然后将行与VS
列相加。
SELECT VS, SUM(TIES) AS TIES
FROM (
(SELECT matches.away_team as VS, count(matches.idmatch) as TIES
from my_football_database.matches WHERE home_team = 14
AND home_team_goals = away_team_goals
AND aet = "N" AND PK = "N"
GROUP BY away_team
LIMIT 5)
UNION ALL
(SELECT matches.home_team as VS, count(matches.idmatch) as TIES
from my_football_database.matches WHERE away_team = 14
AND home_team_goals = away_team_goals
AND aet = "N" AND PK = "N"
GROUP BY home_team
LIMIT 5)) AS u
GROUP BY VS
答案 1 :(得分:0)
使用CASE
构造:
select
otherteam,
count(case when mygoals > othergoals then 1 end) as won,
count(case when mygoals = othergoals then 1 end) as ties,
count(case when mygoals < othergoals then 1 end) as lost
from
(
select
11 as myteam,
case when home_team = 11 then away_team else home_team end as otherteam,
case when home_team = 11 then home_team_goals else away_team_goals end as mygoals,
case when home_team = 11 then away_team_goals else home_team_goals end as othergoals
from mydb.matches
where (home_team = 11 or away_team = 11)
) whoiswho
group by otherteam;
(哎呀,对不起,我猜这比“像一个提示”略多。)