这是我的查询,但结果为false,其中数字行不同,也就是说每当tableA选择2行而tableB选择3结果为假
select sum(tableA.value)+sum(tableB.value1) )
from tableA,tableB
where tableA.data between '2016-01-21' and '2016-03-09'
and tableB.date2 between '2016-01-21' and '2016-03-09'
答案 0 :(得分:1)
您需要在加入之前在子查询中执行求和。一个简单的规则:永远不要在from
子句中使用逗号。
select coalesce(avalue, 0) + coalesce(bvalue, 0)
from (select sum(a.value) as avalue
from tableA a
where a.data between '2016-01-21' and '2016-03-09'
) a cross join
(select sum(b.value) as bvalue
from tableB b
where b.data between '2016-01-21' and '2016-03-09'
) b;
答案 1 :(得分:0)
一个简单的子查询可以来救你。
Select
(Select SUM(value) From tableA
where data between '2016-01-21' and '2016-03-09') +
(Select SUM(value1) From tableB
where date2 between '2016-01-21' and '2016-03-09') FinalValue