求和表的列

时间:2015-01-13 08:29:03

标签: mysql sum

我有两张桌子:

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个表中总计有效负载?

2 个答案:

答案 0 :(得分:0)

试试这样:

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

答案 1 :(得分:0)

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