我们假设我们有3个表A,B和C,其中列district
,population
。{
我需要在单个查询中找到所有表中所有population
的总和
我对查询并不太熟悉,并且尚未建立结构,但想知道下面的查询是否有效。
select Sum(SUM1+SUM2+SUM3)
from tableA A, tableB B, tableC C
where SUM1 = sum(A.population)
and SUM2 = sum(B.population)
and SUM3 = sum(C.population) ;
欢迎任何其他建议。
答案 0 :(得分:4)
您可以使用union all
:
select sum(t.population)
from (
select population from table A
union all
select population from table B
union all
select population from table A
) t
答案 1 :(得分:3)
尝试:
SELECT COALESCE((SELECT SUM(population) FROM TableA),0) +
COALESCE((SELECT SUM(population) FROM TableB),0) +
COALESCE((SELECT SUM(population) FROM TableC),0) AS TotalPopulation