如何计算 3 个不同表中两列的总值?
How to calculate the total value of two column from 3 different tables?
比如我有3张桌子;
表 1:
+-------+
| count |
+-------+
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
+-------+
表 2:
+-------+
| count |
+-------+
| 3 |
| 0 |
| 0 |
| 0 |
| 0 |
+-------+
表 3:
+-------+
| count |
+-------+
| 1 |
| 1 |
| 0 |
| 0 |
| 1 |
+-------+
我要计算table1.count+table2.count+table3.count,得到结果,table_right:
+-------+
| count |
+-------+
| 5 | (1+3+1=5)
| 1 | (0+0+1=1)
| 0 | (0+0+0=0)
| 0 | (0+0+0=0)
| 4 | (3+0+1=4)
+-------+
但是,如果我使用命令:
select table1.count+table2.count+table3.count as total
from table1,table2,table3;
结果将变为:
+-------+
| total |
+-------+
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 4 |
| 3 |
| 3 |
| 3 |
| 6 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 4 |
| 3 |
| 3 |
| 3 |
| 6 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
+-------+
这不是我想要的结果,如果我尝试
select distinct table1.count+table2.count+table3.count as total
from table1,table2,table3;
我会得到:
+-------+
| total |
+-------+
| 5 |
| 4 |
| 7 |
| 2 |
| 1 |
| 3 |
| 6 |
| 0 |
+-------+
还是不是我想要的结果。我怎样才能得到 table_right?
如果您添加一个公共 ID(让我们调用 id rowId 并假设它在每个 table 上具有相同的名称),
SELECT t1.count + t2.count + t3.count AS total
FROM table1 AS t1
LEFT JOIN table2 AS t2 using (rowId)
LEFT JOIN table3 AS t3 using (rowId)
如果你没有这些 id,我只能考虑将所有 t1,然后所有 t2,然后所有 t3 相加,最后将结果加在一起。
SELECT t1+t2+t3 as total
FROM (SELECT (SELECT SUM(count) from table1) as t1,
(SELECT SUM(count) from table2) as t2,
(SELECT SUM(count) from table3) as t3
)
看看这个 SQLFiddle
编辑 (2)
要添加 rowId
只需更改 tables:
ALTER TABLE table1 ADD COLUMN rowId int not null auto_increment primary key;
ALTER TABLE table2 ADD COLUMN rowId int not null auto_increment primary key;
ALTER TABLE table3 ADD COLUMN rowId int not null auto_increment primary key;
比如我有3张桌子; 表 1:
+-------+
| count |
+-------+
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
+-------+
表 2:
+-------+
| count |
+-------+
| 3 |
| 0 |
| 0 |
| 0 |
| 0 |
+-------+
表 3:
+-------+
| count |
+-------+
| 1 |
| 1 |
| 0 |
| 0 |
| 1 |
+-------+
我要计算table1.count+table2.count+table3.count,得到结果,table_right:
+-------+
| count |
+-------+
| 5 | (1+3+1=5)
| 1 | (0+0+1=1)
| 0 | (0+0+0=0)
| 0 | (0+0+0=0)
| 4 | (3+0+1=4)
+-------+
但是,如果我使用命令:
select table1.count+table2.count+table3.count as total
from table1,table2,table3;
结果将变为:
+-------+
| total |
+-------+
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 4 |
| 3 |
| 3 |
| 3 |
| 6 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 4 |
| 3 |
| 3 |
| 3 |
| 6 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 1 |
| 0 |
| 0 |
| 0 |
| 3 |
| 5 |
| 4 |
| 4 |
| 4 |
| 7 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
| 2 |
| 1 |
| 1 |
| 1 |
| 4 |
+-------+
这不是我想要的结果,如果我尝试
select distinct table1.count+table2.count+table3.count as total
from table1,table2,table3;
我会得到:
+-------+
| total |
+-------+
| 5 |
| 4 |
| 7 |
| 2 |
| 1 |
| 3 |
| 6 |
| 0 |
+-------+
还是不是我想要的结果。我怎样才能得到 table_right?
如果您添加一个公共 ID(让我们调用 id rowId 并假设它在每个 table 上具有相同的名称),
SELECT t1.count + t2.count + t3.count AS total
FROM table1 AS t1
LEFT JOIN table2 AS t2 using (rowId)
LEFT JOIN table3 AS t3 using (rowId)
如果你没有这些 id,我只能考虑将所有 t1,然后所有 t2,然后所有 t3 相加,最后将结果加在一起。
SELECT t1+t2+t3 as total
FROM (SELECT (SELECT SUM(count) from table1) as t1,
(SELECT SUM(count) from table2) as t2,
(SELECT SUM(count) from table3) as t3
)
看看这个 SQLFiddle
编辑 (2)
要添加 rowId
只需更改 tables:
ALTER TABLE table1 ADD COLUMN rowId int not null auto_increment primary key;
ALTER TABLE table2 ADD COLUMN rowId int not null auto_increment primary key;
ALTER TABLE table3 ADD COLUMN rowId int not null auto_increment primary key;