MySQL查询将两组结果汇总在一起

时间:2019-06-30 23:37:51

标签: mysql

我运行一个数据库,该数据库保留有关橄榄球裁判员的信息。我想向所有裁判展示裁判所看过的球队,以及比赛的数据。我有MySQL查询返回信息,但是只有当一个团队是主队时,我希望它汇总所有结果并将它们作为一个组合图提供给我。

这是我的查询

SELECT 
  DISTINCT *
FROM (
  SELECT
    T1.Name AS Team, 
    COUNT(M.REF) AS RefCount, 
    SUM(M.HTries) AS Tries, 
    SUM(M.HPT) AS PT, 
    SUM(M.HConv) AS Conv, 
    SUM(M.HPG) AS PG, 
    SUM(M.HDG) AS DG, 
    SUM(M.HYC) AS YC, 
    SUM(M.HRC) AS RC 
  FROM matches M
  LEFT JOIN teams T1 ON T1.TeamID=M.HTeam
  WHERE M.Ref = 2 AND M.date < CURDATE()
  GROUP BY T1.TeamID
UNION 
  SELECT 
    T2.Name as Team,
    COUNT(M.REF) AS RefCount, 
    SUM(M.ATries) AS Tries, 
    SUM(M.APT) AS PT, 
    SUM(M.AConv) AS Conv, 
    SUM(M.APG) AS PG, 
    SUM(M.ADG) AS DG, 
    SUM(M.AYC) AS YC, 
    SUM(M.ARC) AS RC
  FROM matches M
  LEFT JOIN teams T2 ON T2.TeamID=M.ATeam 
  WHERE M.Ref = 2 AND M.date < CURDATE()
  GROUP BY T2.TeamID) AS Teams
GROUP BY Team 
ORDER BY RefCount DESC, Team ASC

它提供的是这样的:

+--------------+----------+-------+------+------+------+------+------+------+
| Team         | RefCount | Tries | PT   | Conv | PG   | DG   | YC   | RC   |
+--------------+----------+-------+------+------+------+------+------+------+
| France       |        4 |     3 |    0 |    3 |    6 |    0 |    1 |    0 |
| Argentina    |        2 |     0 |    0 |    0 |    0 |    0 |    1 |    0 |
| Australia    |        2 |     5 |    0 |    4 |    1 |    0 |    2 |    0 |
| Ireland      |        2 |     2 |    0 |    2 |    4 |    0 |    0 |    0 |
| Wales        |        2 |     4 |    0 |    3 |    5 |    0 |    0 |    0 |
| Barbarians   |        1 |     0 |    0 |    0 |    0 |    0 |    0 |    0 |
| Chiefs       |        1 |     0 |    0 |    0 |    0 |    0 |    0 |    0 |
| Chile        |        1 |     0 |    0 |    0 |    0 |    0 |    0 |    0 |
| Hungary      |        1 |     0 |    0 |    0 |    0 |    0 |    0 |    0 |
| Italy        |        1 |     0 |    0 |    0 |    0 |    0 |    0 |    0 |
| New Zealand  |        1 |     5 |    0 |    4 |    2 |    0 |    0 |    0 |
| Scotland     |        1 |     0 |    0 |    0 |    0 |    0 |    0 |    0 |
| South Africa |        1 |     0 |    0 |    0 |    4 |    2 |    0 |    0 |
| Ukraine      |        1 |     0 |    0 |    0 |    0 |    0 |    0 |    0 |
+--------------+----------+-------+------+------+------+------+------+------+

但是,针对法国的基本查询显示RefCount应该为6(4个主场和2个主场),这适用于所有结果,因为它们仅显示主场总和。

我在做什么错,如何使该查询显示正确的结果?

感谢帮助。

1 个答案:

答案 0 :(得分:0)

在外层进行聚合:

SELECT Team,
       COUNT(RefCount) AS RefCount,
       SUM(Tries) AS Tries,
       SUM(PT) AS PT,
       SUM(Conv) AS Conv,
       SUM(PG) AS PG,
       SUM(DG) AS DG,
       SUM(YC) AS YC,
       SUM(RC) AS RC
FROM
  (SELECT T1.TeamID AS ID,
          T1.Name AS Team,
          M.REF AS RefCount,
          M.HTries AS Tries,
          M.HPT AS PT,
          M.HConv AS Conv,
          M.HPG AS PG,
          M.HDG AS DG,
          M.HYC AS YC,
          M.HRC AS RC
   FROM matches M
   LEFT JOIN teams T1 ON T1.TeamID=M.HTeam
   WHERE M.Ref = 2
     AND M.date < CURDATE()
   UNION SELECT T1.TeamID AS ID,
                T2.Name AS Team,
                M.REF AS RefCount,
                M.ATries AS Tries,
                M.APT AS PT,
                M.AConv AS Conv,
                M.APG AS PG,
                M.ADG AS DG,
                M.AYC AS YC,
                M.ARC AS RC
   FROM matches M
   LEFT JOIN teams T2 ON T2.TeamID=M.ATeam
   WHERE M.Ref = 2
     AND M.date < CURDATE() )
GROUP BY ID
ORDER BY RefCount DESC,
         Team ASC

注意:也许外部别名需要不同。

鉴于主队/客队的结果是相同的,也许Match结构应避免区别,并具有ENUM('Home', 'Away')Opponent UNSIGNED INT(对战游戏的比赛ID)。当然,这将需要更多地考虑其他查询,但是,如果您在此过程的早期,可能值得考虑。