gpt4 book ai didi

Mysql join 与 left join 和 group by

转载 作者:行者123 更新时间:2023-11-30 22:48:55 30 4
gpt4 key购买 nike

我想组合三个 sql 查询,下面给出查询,site 和 goods 列是公共(public)列,我的结果中想要 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 join 与 left join 和 group by,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28734951/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com