SQL中如何将两个查询结果相加?

查询结果A:
names sale
mike 10
查询结果B:
names sale
mike 10
请问,如何写SQL语句查询语句,能直接得出累加后的结果,例如:
得出查询结果C:
names sale
mike 20
或者查询结果D:
names all_sale
mike 20
小弟是初学者,还请多多帮忙,多谢。

做个简单的。
两个数据表db1,db2

查询结果A是从数据表db1获取的:
select names, sale from db1

查询结果B是从数据表db2获取的:
select names, sale from db2

则查询结果C:

select names,sum(sale) as sale
from
(select names, sale from db1
union
select names, sale from db2
)
group by names
温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-09-10
表A 表B
select ABC from A where ID=xxx
select ABC from B where nane = xxx
求和
select sum(ABC) from (select ABC from A where ID=xxx union select ABC from B where nane = xxx)
相似回答