这是MySQL表
Amount | DebitAccount | CreditAccount | TransactionPartnerName
--------------------------------------------------------------
1 | 5610 | 5720 | name one
150 | 7210 | 5610 | name one
需要得到这样的东西
name one result for account 5610 = 149.
((`CreditAccount` for `5610`) - (`DebitAccount` for `5610`)) = 149
换句话说:
1)总和Amounts
的地方和DebitAccount
是5610
TransactionPartnerName
name one
2)总结所有Amounts
地方CreditAccount
的5610
和是TransactionPartnerName
name one
3) 从CreditAccount
总和减去DebitAccount
总和
试过这段代码
SELECT TransactionPartnerName, SUM(Amount)
FROM ( SELECT * FROM 2_1_journal) DATA
WHERE (CAST(DebitAccount AS UNSIGNED) IN (?) OR CAST(CreditAccount AS UNSIGNED) IN (?))
GROUP BY TransactionPartnerName
但结果为 account 5610
get value 151
。代表着SUM(Amount)
这和DebitAccount
。CreditAccount
想了想怎么修改。
尝试过这样的事情
SELECT TransactionPartnerName,
IFNULL( d.Amount, 0 ) - IFNULL( c.Amount, 0 ) AS Amount
FROM
(
SELECT DebitAccount,
SUM( Amount ) AS Amount FROM
( SELECT * FROM 2_1_journal) DATA
GROUP BY DebitAccount
) d ON (AccountNumber = d.DebitAccount)
(
SELECT CreditAccount,
SUM( Amount ) AS Amount FROM
( SELECT * FROM 2_1_journal) DATA
GROUP BY CreditAccount
) c ON (AccountNumber = c.CreditAccount)
GROUP BY TransactionPartnerName
但得到空白页....
请告知需要纠正的地方