我正在开发一个小型会计应用。以下函数用于获取供应商的交易。我希望在此循环中的每个事务之后获得平衡。有人可以为我提供最好的方法。
public function transaction_by_id_paginated($supplier_id,$start_from,$limit) {
$sql="
SELECT *
FROM transactions
WHERE tr_sup_id = $supplier_id
ORDER
BY tr_date DESC
LIMIT $start_from, $limit
";
$suppliers = DB::fetch($sql);
return $suppliers;
}
使用此功能,我想得到一个计算的余额值。我不是在DB中存储余额。
transactions
表中的字段
tr_id (Primary) int(11)
tr_sup_id int(11)
bill_id int(11)
tr_date date
tr_type varchar(250)
tr_bill int(250)
tr_payment int(250)
tr_payment_note varchar(250)
我希望余额等于balance = balance + tr_payment - tr_bill
答案 0 :(得分:0)
请考虑以下事项:
SELECT * FROM ints;
+---+
| i |
+---+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 |
+---+
SELECT i, @j:=@j+i FROM ints, (SELECT @j:=0) vars ORDER BY i;
+---+----------+
| i | @j:=@j+i |
+---+----------+
| 0 | 0 |
| 1 | 1 |
| 2 | 3 |
| 3 | 6 |
| 4 | 10 |
| 5 | 15 |
| 6 | 21 |
| 7 | 28 |
| 8 | 36 |
| 9 | 45 |
+---+----------+
10 rows in set (0.00 sec)
SELECT i, @j:=@j+i FROM ints, (SELECT @j:=0) vars ORDER BY i LIMIT 3,6;
+---+----------+
| i | @j:=@j+i |
+---+----------+
| 3 | 3 |
| 4 | 7 |
| 5 | 12 |
| 6 | 18 |
| 7 | 25 |
| 8 | 33 |
+---+----------+
6 rows in set (0.00 sec)