我想结合三个sql查询,查询如下,site和goods列是普通列,我要在我的结果中有9列,如果您一一运行这三个查询,您将得到3个结果有3列,我想要结合这三个结果
select distinct
a.site,a.goods,
sum(a.quantity)
from
stock_consumed a
group by
a.site,a.goods
select distinct
b.site,
b.goods,sum(b.quantity)
from
opening_balance b
group by
b.site,b.goods
select distinct
c.site,
c.goods,sum(c.no_of_units)
from
inward_stock c
group by
c.site,c.goods
最佳答案
您正在寻找union
。它允许您组合两个查询的结果,只要它们的结果具有相同的列数即可。
select distinct a.site,a.goods,sum(a.quantity) from stock_consumed a group by a.site,a.goods
union
select distinct b.site,b.goods,sum(b.quantity) from opening_balance b group by b.site,b.goods
union
select distinct c.site,c.goods,sum(c.no_of_units) from inward_stock c group by c.site,c.goods
关于mysql - MySQL的加入与左联接和分组依据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28734951/