对 table 的列求和

Sum the column of the table

下面有 2 个表格:

Table1
+-----------+------------+-----------+-----------+
| date      | TRAFFIC1   | PAYLOAD11 | PAYLOAD12 |
+-----------+------------+-----------+-----------+
|2015-01-01 | 2          | 2         | 2         |
|2015-01-01 | 3          | 3         | 3         |
|2015-01-02 | 2          | 2         | 2         |
|2015-01-02 | 3          | 3         | 3         |
+-----------+------------+-----------+-----------+

Table2
+-----------+------------+-----------+-----------+
| date      | TRAFFIC21  | PAYLOAD21 | PAYLOAD22 |
+-----------+------------+-----------+-----------+
|2015-01-01 | 2          | 2         | 2         |
|2015-01-01 | 3          | 3         | 3         |
|2015-01-02 | 2          | 2         | 2         |
|2015-01-02 | 3          | 3         | 3         |
+-----------+------------+-----------+-----------+

我想按日期对上面 2 个表中的有效负载进行总和?

这样试试:

SELECT SUM(t1.payload)+SUM(t2.payload) AS total FROM table1 AS t1,table2 AS t2 GROUP BY date

更多信息请查看:Query SUM for two fields in two different tables

SELECT date, SUM(payload) FROM (
SELECT date, `PAYLOAD11` + `PAYLOAD12` as payload FROM Table1
union all
SELECT date, `PAYLOAD21` + `PAYLOAD22` as payload FROM Table2) grp
GROUP BY date

SQLFIDDLE