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

2024年11月29日 02:53
有2个网友回答
网友(1):

做个简单的。
两个数据表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

网友(2):

表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)