单独的表和加入

时间:2014-11-04 06:49:25

标签: sql database join sum union

我有以下表格,其中包含一些ddata,如下所述

表A

------------------------------------ 
      date    |  cnt  |  click  |  point 
------------------------------------ 
2014-07-01 |  3    |    5    |    10 

2014-07-03 |  5    |    15    |    20 

2014-07-05 |  1    |    2    |    3 

表B

------------------------------------ 
      date    |  cnt  |  click  |  point 
------------------------------------ 
2014-07-02 |  3    |    5    |    10 

2014-07-03 |  3    |    5    |    20 

2014-07-06 |  1    |    2    |    3 

表C

------------------------------------ 
      date    |  cnt  |  click  |  point 
------------------------------------ 
2014-07-01 |  3    |    5    |    10 

2014-07-02 |  2    |    3    |    30 

2014-07-05 |  1    |    2    |    3 

结果

------------------------------------ 
      date    |  cnt  |  click  |  point 
------------------------------------ 
2014-07-01 |  6    |    10    |    20 

2014-07-02 |  5    |    8      |    40 

2014-07-03 |  8    |    20    |    40 

2014-07-05 |  2    |    4    |    6 

2014-07-06 |  1    |    2    |    3 

以上加入结果但不是联合所有。 有什么建议吗?

1 个答案:

答案 0 :(得分:0)

你可以union all所有三个表并总结结果:

SELECT   `date`, SUM(cnt), SUM(click), SUM(point)
FROM     (SELECT `date`, cnt, click, point FROM a
          UNION ALL
          SELECT `date`, cnt, click, point FROM b
          UNION ALL
          SELECT `date`, cnt, click, point FROM c) t
GROUP BY `date`