gpt4 book ai didi

mysql - 如何将两个查询结果合并到一个表中

转载 作者:行者123 更新时间:2023-11-29 06:37:26 28 4
gpt4 key购买 nike

我有两个查询给出两个差异结果,并且两个结果表都有一个公共(public)列,我想将它们合并,并有一个结果表将公共(public)列合并为一列

这是我的疑问

    query1=  select b.CUSTOMERDESCRIPTOR as outlet,sum(a.netamount) as amount from syncbill a, ecustomer b where a.outlet=b.CUSTOMERIDENTIFIER and a.cancelled<>'Y' and year(curdate())=year(a.billdate) and month(curdate())=month(a.billdate) group by a.OUTLET;


query2= select b.CUSTOMERDESCRIPTOR as outlet,sum(a.netamount) as amount from syncbill a, ecustomer b where a.outlet=b.CUSTOMERIDENTIFIER and a.cancelled<>'Y' and year(curdate())=year(a.billdate) group by a.OUTLET;

第一个查询给了我这样的结果

this is the result i am getting from query1

我的第二个查询给出的结果如下

[This is the result i am getting from querry2[2]

现在我想要的是合并这两个查询以获得这样的结果

i want this kind of result

我尝试过联合,但它重复数据并仅显示两列的表格。请任何了解 Mysql 的人请指导我如何实现这一目标

最佳答案

请运行以下查询,您将一次获得两列

SELECT b.customerdescriptor AS outlet, 
Sum(a.netamount) AS currentYearAmount,
Sum(CASE
WHEN Month(Curdate()) = Month(a.billdate) THEN a.netamount
ELSE 0
end) AS CurrentMonth
FROM syncbill a,
ecustomer b
WHERE a.outlet = b.customeridentifier
AND a.cancelled <> 'Y'
AND Year(Curdate()) = Year(a.billdate)
GROUP BY a.outlet;

关于mysql - 如何将两个查询结果合并到一个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53167877/

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