gpt4 book ai didi

mysql - 在Mysql中连接2个销售表

转载 作者:行者123 更新时间:2023-11-29 23:19:40 25 4
gpt4 key购买 nike

我有 2 张表交易现金。我将在第一天开具发票,现金可能会分期或多期收到。

我的表格如下所示:

交易表:

date       | customerid | inv_amt 
---------------------------------
2014-12-12 | 1001 | 1000
2014-12-12 | 1002 | 2000

现金表:

date       | customerid | rec_amt 
---------------------------------
2014-12-12 | 1001 | 1000
2014-12-12 | 1002 | 1000
2014-12-13 | 1002 | 500
2014-12-13 | 1003 | 2000

我想加入如下:

date       | customerid  | Inv_amt | cash
-----------------------------------------
2014-12-12 | 1001 | 1000 | 1000
2014-12-12 | 1002 | 2000 | 1000
2014-12-13 | 1002 | Null | 500
2014-12-13 | 1003 | Null | 2000

最佳答案

试试这个:

SELECT    a.dates, a.customerid, sum(a.inv_amt), sum(b.rec_amt)
FROM trans a
LEFT JOIN cash b ON a.dates = b.dates AND a.customerid = b.customerid
GROUP BY a.dates, a.customerid

UNION

SELECT b.dates, b.customerid, sum(a.inv_amt), sum(b.rec_amt)
FROM trans a
RIGHT JOIN cash b ON a.dates = b.dates AND a.customerid = b.customerid
GROUP BY b.dates, b.customerid

关于mysql - 在Mysql中连接2个销售表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27437758/

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