sql查询将2个sum()结果添加到单个总列中

时间:2014-01-07 22:12:27

标签: mysql sql

我试图在我的工作台客户端中提取sql查询,我使用两个表a > col1, col2table b > colx, coly。我想要实现的是表sum()中的a > col a值和表sum()中的b > coly值,接下来我想将两者的结果相加。

SQL查询

select

sum(table_a.col1) as 'result 1'
sum(table_b.coly) as 'result 2'

'现在我如何将这两个结果添加到新列中并使所有三列可见,例如列:结果1,结果2,总计'

2 个答案:

答案 0 :(得分:3)

select

sum(table_a.col1) as 'result 1'
sum(table_b.coly) as 'result 2',
sum(table_a.col1) + sum(table_b.coly) as Total

答案 1 :(得分:0)

我怀疑你想要的查询是:

select a.result1, a.result2, coalesce(a.result1, 0) + coalesce(b.result2)
from (select sum(table_a.col1) as result1 from table_a) a cross join
     (select sum(table_b.coly) as result2 from table_b) b;

查询的原始结构会导致两个表之间出现笛卡尔积。