使用不同行中的下一列减去列

时间:2014-05-28 01:44:11

标签: php mysql subtraction

有没有办法使用PHP或MySQL减去不同行中的两列?

|MATRIX NO|DESCRIPTIONS                   |DEBIT  | CREDIT  |
|CA10045  |STUDIES FEES                   |500.00 | 0.00    |
|CA10045  |OUTSTANDING LAST SEMESTER FEES |300.00 | 0.00    |
|CA10045  |STUDIES FEES                   |0.00   | 300.00  |
|CA10045  |OUTSTANDING LAST SEMESTER FEES |0.00   | 100.00  |

第1行借记研究费用第3行研究费用扣除

1 个答案:

答案 0 :(得分:0)

不是最优雅的解决方案,但无论如何它都在这里。

SELECT a.matrix_no, a.descriptions, a.debit - b.credit as total
FROM (select * from matrix where debit > 0) a
INNER JOIN (select * from matrix where credit > 0) b
ON a.matrix_no = b.matrix_no AND a.descriptions = b.descriptions