【问题标题】:How to calculate the total value of two column from 3 different tables?如何从 3 个不同的表中计算两列的总值?
【发布时间】:2015-02-23 23:09:21
【问题描述】:

例如,我有 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 或您可以分组的东西?
  • 表格代表无序集。行之间没有对应关系,除非你有列来表达这种关系。
  • 你的意思是给每个表添加一些rowID?如何为每个表添加自动增加 num 的 rowID?
  • @josegomezr 我可以选择添加一个通用 ID,例如行 ID 或其他内容。但是如何添加呢?
  • 如果你添加一个通用 id(让我们调用 id rowId 并假设它在每个表上都具有相同的名称),那么它就更容易了,只需 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)

标签: mysql add


【解决方案1】:

如果您添加一个通用 id(让我们调用 id rowId 并假设它在每个表上都具有相同的名称),

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,只需更改表格:

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;

【讨论】:

  • 这三个表如何添加row id?
  • ALTER TABLE ADD COLUMN rowId int not null; ERROR 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“ ADD COLUMN rowId int not null”附近使用正确的语法 我最初的需要是如何将 ROWID 添加到 table1、table2、table3 中并让他们拥有已插入 INCREASING num?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-23
  • 2019-09-14
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
相关资源
最近更新 更多