我需要在 MySQL 中为此查询创建 JOIN 语法:
SELECT
TblSupplier.SupplierName AS SupplierName,
(SUM(TblBuy.TotalBuy) - SUM(TblReturn.TotalReturn)) AS Total
(SUM(TblPayment.TotalPaymentToday) - SUM(TblReturnItem.TotalReturnToday)) AS TotalPayment,
((SUM(TblBuy.TotalBuy) - SUM(TblReturn.TotalReturn)) - (SUM(TblPayment.TotalPaymentToday) - SUM(TblReturnItem.TotalReturnToday))) AS Balance
FROM ...
GROUP BY TblSupplier.ID
但我不知道该怎么做...
我的问题示例:
INSERT INTO TableSupplier (ID, SupplierName) VALUES (1, 'Company A'), (2, 'Company B');
插入 TableBuy(ID、SupplierID、TotalBuy)值 (1, 1, 100), (2, 1, 200), (3, 2, 50), (4, 2, 100), (5, 2, 50) ;
插入 TablePayment(ID、BuyID、TotalPaymentToday)值(1、1、50)、(2、3、50);
INSERT INTO TableReturn (ID, SupplierID, TotalReturn) VALUES (1, 1, 20), (2, 2, 30), (3, 2, 10);
INSERT INTO TableReturnItem (ID, ReturnID, TotalReturnToday) VALUES (1, 1, 10), (2, 2, 30);
我按照这个例子,我得到了这个结果:
mysql> select
-> ts.SupplierName AS SupplierName,
-> (SUM(tb.TotalBuy) - SUM(tr.TotalReturn)) AS Total,
-> (SUM(tp.TotalPaymentToday) - SUM(tri.TotalReturnToday)) 作为 TotalPayment ,
-> ((SUM(tb.TotalBuy) - SUM(tr.TotalReturn)) -
-> (SUM(tp.TotalPaymentToday) - SUM(tri.TotalReturnToday))) AS Balance
-> from tablesupplier ts
-> 内部连接 tablebuy ts.id=tb.id 上的 tb
-> ts.id=tr.id 上的内连接 tablereturn tr
-> ts.id=tp.id 上的内连接 tablepayment tp
-> ts.id=tri 上的内连接 tablereturnitem tri。 id
-> 按 ts.id 分组;
供应商名称 | 总计 | 总付款 | 平衡
-------------------------------------------------
公司一个 | 80 | 40 | 40
B 公司 | 170 | 20 | 150
我想要的结果是:
供应商名称 | 总计 | 总付款 | 余额
------------------------------------------------
A公司| 280 | 40 | 240
B 公司 | 160 | 20 | 140