我如何加入多个表,其中一些(不是全部)列相同并且 return 每列的总和?

How do I JOIN multiple tables where some, not all, columns are the same and return the sum of each column?

我有多个表跟踪来自不同位置(即寒冷与温暖)的信息,这些信息根据季节提供服务,并且需要创建这些位置的企业级视图。

查看 UNIONS 和 JOINS 的描述,看起来这些表必须具有相同的列数或相同的列名。我需要的是一个结果,它提供相同列的总和以及使用 PHP MySQL.

唯一的列的总和

Table1
+-------+-------+-------+
| col-A | col-B | col-C |
+-------+-------+-------+
|   2   |    8  |   5   |
+-------+-------+-------+
|   1   |    9  |   7   |
+-------+-------+-------+
|   3   |    2  |   3   |
+-------+-------+-------+

Table2
+-------+-------+-------+
| col-1 | col-2 | col-3 |
+-------+-------+-------+
|   2   |    1  |   6   |
+-------+-------+-------+
|   5   |    5  |   4   |
+-------+-------+-------+
|   2   |    7  |   3   |
+-------+-------+-------+

Table3
+-------+-------+-------+-------+-------+-------+
| col-A | col-B | col-1 | col-2 | col-3 | col-4 |
+-------+-------+-------+-------+-------+-------+
|   9   |    4  |   1   |   2   |    1  |   4   |
+-------+-------+-------+-------+-------+-------+
|   2   |    6  |   6   |   8   |    3  |   6   |
+-------+-------+-------+-------+-------+-------+
|   4   |    7  |   3   |   2   |    4  |   5   |
+-------+-------+-------+-------+-------+-------+

Result Table
+-------+-------+-------+-------+-------+-------+-------+
| col-A | col-B | col-C | col-1 | col-2 | col-3 | col-4 |
+-------+-------+-------+-------+-------+-------+-------+
|   21  |   36  |   15  |   19  |   25  |   21  |   15  |
+-------+-------+-------+-------+-------+-------+-------+

我认为这可以用 UNION 来完成,如下所示:

SELECT SUM(`col-A`) AS 'Col-A',SUM(`col-b`) AS 'Col-B',
       SUM(`col-C`) AS 'Col-C',SUM(`col-1`) AS 'Col-1',
       SUM(`col-2`) AS 'Col-2',SUM(`col-3`) AS 'Col-3',SUM(`col-4`) AS 'Col-4' 
  FROM
(SELECT `col-A`,`col-b`,0 AS 'col-c',`col-1`,`col-2`,`col-3`,`col-4` FROM table_3 UNION
SELECT 0,0,0,`col-1`,`col-2`,`col-3`,0 FROM table_2 UNION
SELECT `col-A`,`col-b`,`col-C`, 0,0,0,0 FROM table_1 ) XX

因为table_3的列数最多,所以我把它放在并集之上。以下查询只需要在 table 中不存在的列中填充 0(零)即可满足并集要求。