請幫助,我有一個表像這樣我的用戶的平衡:獲取在同一個表
| ID | userId | amount | type |
-------------------------------------
| 1 | 10 | 10 | expense |
| 2 | 10 | 22 | income |
| 3 | 3 | 25 | expense |
| 4 | 3 | 40 | expense |
| 5 | 3 | 63 | income |
我正在尋找一種方式來使用一個查詢和retrive每個用戶的平衡。
困難的部分來自於金額必須加在費用上而減去收入。
這將是結果表:
| userId | balance |
--------------------
| 10 | 12 |
| 3 | -2 |
這也適用,謝謝Gordon – 2012-08-09 14:09:23