如何将同一数据库中三个表的三个字段数据相加

时间:2011-08-17 21:42:18

标签: sum field

我有一张名为Sound的表

id name hit

  • 100 sam 12
  • 101 zad 10

和其他表格像这样抨击SoundK

id name hit

  • 102 qwe 12
  • 103 rty 10

和其他表格像这样的SoundD

id name hit

  • 104 asd 12
  • 105 xcv 10

所以如何计算所有三个表Sound + SoundK + SoundD

的字段(命中)为总数

提前致谢

RGDS for All

2 个答案:

答案 0 :(得分:0)

以通用方式,您可以将三个表合并在一起,因为它们共享相同的数据,然后进行求和。

select sum(hit)
from (
    select hit
    from Sound
    union
    select hit
    from SoundK
    union
    select hit
    from SoundD
)

答案 1 :(得分:0)

SELECT SUM (soundsum) FROM (
SELECT SUM(hit) AS SoundSum FROM Sound AS s
UNION ALL
SELECT SUM(hit) AS SoundSum FROM SoundK AS s
UNION ALL
SELECT SUM(hit) AS SoundSum FROM SoundD AS s
) X