2015-02-23 136 views
0

例如,我有3個表格; 表1:如何計算3個不同表格中兩列的總值?

+-------+ 
| 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?

+1

做此列共用一個ID或東西,你可以通過...分組? – josegomezr 2015-02-23 23:13:05

+0

表格代表*無序*集合。行之間沒有對應關係,除非您有列來表達這種關係。 – 2015-02-23 23:16:59

+0

你是否想爲每個表添加一些rowID?如何爲每個表添加自動增加num的rowID? – beasone 2015-02-23 23:23:27

回答

2

如果添加了一個共同的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

EDIT(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; 
+0

如何將行ID添加到這三個表? – beasone 2015-02-23 23:53:13

+0

查看答案更新 – josegomezr 2015-02-24 01:25:30

+0

ALTER TABLE ADD COLUMN rowId int not null; 錯誤1064(42000):您的SQL語法錯誤;檢查對應於你的MySQL服務器版本的手冊,在第1行使用' ADD COLUMN rowId int not null'使用正確的語法。我最初的需求是如何將ROWID添加到table1,table2,table3中並讓它們插入增加數字? – beasone 2015-02-24 02:29:02

相關問題