我有两张桌子。我想将表1中一列的所有值添加到表2的特定列的所有值。如何做?
例如
表A
Id Name Salary
1 A 20000
2 B 30000
表B
Id Name Salary
1 A 30000
2 B 40000
我希望结果为total salary= sum(salary of table A) + sum(salary of table B)
此结果应为120000
答案 0 :(得分:1)
使用子查询执行此操作:
select a.s + b.s
from (select sum(salary) as s from a) cross join
(select sum(salary) as s from b);
或者,如果其中一个表可能为空,union all
会更好:
select sum(salary)
from (select id, name, salary from a union all
select id, name, salary from b
) ab;
答案 1 :(得分:-1)
首先使用联盟。试试这个问题:
select sum(salary) from
(
select sum(Salary) as salary from tableA
union
select sum(Salary) as salary from tableB
)