简体   繁体   English

如何将两个嵌套的MySQL查询合并到一个视图中?

[英]How do I combine two nested MySQL queries into one View?

I have two queries, almost similar, but never the less,They must be treated as separate as they have different meanings and values, I want to combine them into one view, I tied doing UNION , but the result was they were all combined into one table, which is not what I want, I would like them to appear as entirely separate tables under one view , here is what I did: 我有两个查询,几乎是相似的,但从来没有少过。由于它们具有不同的含义和价值,因此必须将它们分开对待,我想将它们组合成一个视图,我捆绑了UNION ,但结果是它们都被组合成一个表,这不是我想要的,我希望它们在一个视图下显示为完全独立的表 ,这是我所做的:

CREATE VIEW TEAM_SUMMARY AS
 SELECT DISTINCT COUNTRY.country_name AS CountryName_T1,count(Team1)AS NoOfGames,
    SUM(Team1_score) AS TotalGoalsFor,SUM(Team2_score) AS TotalGoalsAgainst
    FROM COUNTRY,MATCH_RESULTS WHERE
    country_name = Team1
    group by country_name


    UNION 

 SELECT DISTINCT COUNTRY.country_name AS CountryNameT_2,count(Team2)AS NoOfGames,
    SUM(Team2_score) AS TotalGoalsFor,SUM(Team1_score) AS TotalGoalsAgainst
    FROM COUNTRY,MATCH_RESULTS WHERE
    country_name = Team2
    group by country_name;

UPDATE: So, the output of my current query is something like this: 更新:因此,我当前查询的输出是这样的:

mysql> SELECT * FROM TEAM_SUMMARY;
+----------------------+-----------+---------------+-------------------+
| CountryName          | NoOfGames | TotalGoalsFor | TotalGoalsAgainst |
+----------------------+-----------+---------------+-------------------+
| Algeria              |         1 |             1 |                 1 |
| Argentina            |         4 |             5 |                 1 |
| Australia            |         2 |             2 |                 6 |
| Belgium              |         3 |             5 |                 2 |
| Bosnia & Herzegovina |         1 |             3 |                 1 |
| Brazil               |         6 |             7 |                13 |
| Cameroon             |         2 |             1 |                 8 |
| Chile                |         1 |             3 |                 1 |
| Columbia             |         3 |             7 |                 1 |
| Costa Rica           |         2 |             1 |                 1 |
| Croatia              |         1 |             1 |                 3 |
| Ecuador              |         1 |             0 |                 0 |
| England              |         1 |             1 |                 2 |
| France               |         3 |             5 |                 1 |
| Germany              |         4 |             9 |                 3 |
| Ghana                |         1 |             1 |                 2 |
| Greece               |         1 |             2 |                 1 |
| Honduras             |         2 |             1 |                 5 |
| Iran                 |         1 |             0 |                 0 |
| Italy                |         2 |             0 |                 2 |
| Ivory Coast          |         1 |             2 |                 1 |
| Japan                |         2 |             1 |                 4 |
| Mexico               |         1 |             1 |                 0 |
| Netherlands          |         4 |             4 |                 1 |
| Nigeria              |         2 |             3 |                 3 |
| Portugal             |         1 |             2 |                 1 |
| Russia               |         1 |             1 |                 1 |
| South Korea          |         2 |             2 |                 5 |
| Spain                |         2 |             1 |                 7 |
| Switzerland          |         2 |             4 |                 6 |
| Uruguay              |         2 |             3 |                 4 |
| USA                  |         2 |             2 |                 3 |
| Algeria              |         3 |             6 |                 6 |
| Argentina            |         3 |             3 |                 3 |
| Australia            |         1 |             1 |                 3 |
| Belgium              |         2 |             1 |                 1 |
| Bosnia & Herzegovina |         2 |             1 |                 3 |
| Brazil               |         1 |             4 |                 1 |
| Cameroon             |         1 |             0 |                 1 |
| Chile                |         3 |             3 |                 3 |
| Columbia             |         2 |             5 |                 3 |
| Costa Rica           |         3 |             4 |                 1 |
| Croatia              |         2 |             5 |                 3 |
| Ecuador              |         2 |             3 |                 3 |
| England              |         2 |             1 |                 2 |
| France               |         2 |             5 |                 2 |
| Germany              |         3 |             9 |                 1 |
| Ghana                |         2 |             3 |                 4 |
| Greece               |         3 |             1 |                 4 |
| Honduras             |         1 |             0 |                 3 |
| Iran                 |         2 |             1 |                 4 |
| Italy                |         1 |             2 |                 1 |
| Ivory Coast          |         2 |             2 |                 4 |
| Japan                |         1 |             1 |                 2 |
| Mexico               |         3 |             4 |                 3 |
| Netherlands          |         3 |            11 |                 3 |
| Nigeria              |         2 |             0 |                 2 |
| Portugal             |         2 |             2 |                 6 |
| Russia               |         2 |             1 |                 2 |
| South Korea          |         1 |             1 |                 1 |
| Spain                |         1 |             3 |                 0 |
| Switzerland          |         2 |             3 |                 1 |
| Uruguay              |         2 |             1 |                 2 |
| USA                  |         2 |             3 |                 3 |
+----------------------+-----------+---------------+-------------------+
64 rows in set (0.01 sec)

UPDATE2: Each query provides 32 row, and here they are combined into 64 rows so I don't know which belongs to which query, you can see that USA is the last row of each query and then it starts with Algeria again for the second query with different values that do not represent the column description. UPDATE2:每个查询提供32行,在这里它们被合并为64行,所以我不知道哪个属于哪个查询,您可以看到USA是每个查询的最后一行,然后第二个查询从Algeria开始使用不代表列描述的不同值进行查询。

What I want is something like this: 我想要的是这样的:

+------+--------+    +------+--------+
| code | SUM(*) |    | code | SUM(*) |
+------+--------+    +------+--------+
| AAA  |      4 |    | AAA  |      4 |
| BBB  |      3 |    | CCC  |      1 |
+------+--------+    +------+--------+

Then I did some searching in order to use JOIN as shown here Combine results of two unrelated queries into single view but, this scenario is much less complicated than mine and couldn't apply it on my scenario, Any Idea? 然后,我进行了一些搜索以使用JOIN ,如下所示: 将两个不相关查询的结果合并到单个视图中,但是,这种情况比我的情况复杂得多,并且无法将其应用于我的情况下,任何想法?

One view doesn't product two result sets. 一个视图不会产生两个结果集。 But you can identify where they come from: 但是您可以确定它们的来源:

CREATE VIEW TEAM_SUMMARY AS
   SELECT 'Team1' as which,
          c.country_name AS CountryName_T1, count(Team1) AS NoOfGames,
          SUM(Team1_score) AS TotalGoalsFor,
          SUM(Team2_score) AS TotalGoalsAgainst
    FROM COUNTRY c JOIN
         MATCH_RESULTS mr
         ON c.country_name = mr.Team1
    GROUP BY country_name
    UNION ALL
    SELECT 'Team2' as which,
           c.country_name AS CountryNameT_2,
           count(Team2) AS NoOfGames,
           SUM(Team2_score) AS TotalGoalsFor,
           SUM(Team1_score) AS TotalGoalsAgainst
    FROM COUNTRY c JOIN
         MATCH_RESULTS mr
         ON c.country_name = mr.Team2
    GROUP BY country_name;

Notes: 笔记:

  • SELECT DISTINCT with GROUP BY is almost always unnecessary (as in this case. 使用GROUP BY SELECT DISTINCT几乎总是没有必要的(在这种情况下。
  • Use UNION ALL by default. 默认情况下使用UNION ALL Only use UNION when you specifically want to incur the overhead for removing duplicates. 仅在您特别想承担删除重复项的开销时才使用UNION
  • Table aliases make the query easier to write and to read. 表别名使查询更易于编写和阅读。
  • The above adds a column which to specify where each row comes from. 上面添加了一个列which用于指定每行的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM