0
我從一個MySQL表中讀取一些事務性的數據是這樣的:如何返回相繼排差異與其他數據(MySQL的)一列
SELECT bill_no as Document_No, bill_date as Trans_Date, bill_amount as Amount, 0 as Balance
FROM bill_table
WHERE consumer_name = 'John'
UNION
SELECT receipt_no as Document_No, receipt_date as Trans_Date, -receipt_amount as Amount, 0 as Balance
FROM receipt_table
WHERE consumer_name = 'John'
ORDER BY Trans_Date
導致這樣的事情
+-----------+----------+------+-------+ |Document_No|Order_date|Amount|Balance| +-----------+----------+------+-------+ |BILL58788 |2010-08-09|493 | 0 | |BILL58789 |2010-08-10|789 | 0 | |REC_12379 |2010-08-11|-1282 | 0 | |BILL58788 |2010-08-12|1493 | 0 | |BILL58788 |2010-09-01|4930 | 0 | |REC_12380 |2010-10-02|-2000 | 0 | +-----------+----------+------+-------+
這給我所有的賬單&約翰的收據細節。所有金額在同一欄中,帳單金額爲正數&收據金額爲負數。
在最後一列「平衡」,我希望有一個總的連續大量像這樣的,動態計算:
+-----------+----------+------+-------+ |Document_No|Order_date|Amount|Balance| +-----------+----------+------+-------+ |BILL58788 |2010-08-09|493 | 493 | |BILL58789 |2010-08-10|789 | 1282 | |REC_12379 |2010-08-11|-1282 | 0 | |BILL58788 |2010-08-12|1493 | 1493 | |BILL58788 |2010-09-01|4930 | 6423 | |REC_12380 |2010-10-02|-2000 | 4423 | +-----------+----------+------+-------+
我知道我可以在PHP中獲取數據後,做到這一點,但我想這樣做mySQL本身。
可能嗎?請幫忙。 在此先感謝。
那精美的作品。謝謝。我不熟悉在mySQL中使用變量。你能告訴我的目的: (select @balance:= 0)const – Xaq