1

我有两个 SELECT 语句给出值“TotalSales”和“VendorPay_Com”。我希望能够在一个 MySQL 语句中从 TotalSales 中减去 VendorPay_Com 以获得值“Outstanding_Funds”,但我还没有找到一种可靠的方法来做到这一点。

这是我的两个陈述:

查询一:

SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = 'Sold';

查询 2:

SELECT SUM(AC.Amount) AS VendorPay_Comm
FROM (
SELECT Amount FROM lawyer_pays_vendor
UNION ALL
SELECT CommissionEarned AS `Amount` FROM COMMISSION AS C WHERE C.`status` = 'Paid') AS AC

对此事的任何帮助将不胜感激:)

4

2 回答 2

13

你可以这样做:

select (select ...) - (select ...)

在您的示例中,只需:

select 

(
SELECT SUM(Price) AS TotalSales
FROM PROPERTY
WHERE Status = 'Sold'
)
-
(
SELECT SUM(AC.Amount) AS VendorPay_Comm
FROM (
SELECT Amount FROM lawyer_pays_vendor
UNION ALL
SELECT CommissionEarned AS `Amount` FROM COMMISSION AS C WHERE C.`status` = 'Paid') AS AC
) AS Outstanding_Funds
于 2013-02-20T05:16:53.327 回答
1

尝试

SELECT TotalSales-VendorPay_Comm AS Outstanding_Funds
  FROM
(SELECT SUM(Price) AS TotalSales
   FROM PROPERTY
  WHERE Status = 'Sold') t1, 
(SELECT SUM(Amount) AS VendorPay_Comm
   FROM (SELECT Amount FROM lawyer_pays_vendor
          UNION ALL
         SELECT CommissionEarned AS Amount 
           FROM COMMISSION
          WHERE Status = 'Paid') t0) t2

这是sqlfiddle

于 2013-02-20T05:15:41.703 回答