DE_CR DE_CR - 1 month ago 8
MySQL Question

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:

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.


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?

Answer

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.
  • Use UNION ALL by default. Only use UNION when you specifically want to incur the overhead for removing duplicates.
  • Table aliases make the query easier to write and to read.
  • The above adds a column which to specify where each row comes from.
Comments