嗨大家我一直在编码并且不能很好地解决这个问题...... 样品: 从BILLING表中可以看到2张账单,费用分别为1月1,500和2月2,000 从PAYMENT表中,客户支付了2,000,这笔金额应扣除到1月BILL和2月账单,因为2,000比1月的金额大,所以应该自动扣除500到下一个BILL 2月左右。 表
OUT PUT
AMOUNT PAID - 2,000
| MONTH | BILL | AMT PAID | BALANCE |
-------------------------------------------------------
| January | 1,500 | 2,000 | -500 |
| February | 2,000 | 500 | 1,500 |
QUERY
SQL BILLING
| MONTH | BILL |
| JAN | 1500 |
| FEB | 2000 |
$query = SELECT MONTH, BILL FROM billing
$amt_paid = 2000;
foreach($query as $row){
$actual_paid = $amt_paid;
$actual_bal -= $actual_paid;
echo '<tr>';
echo '<td>'.$row->MONTH.'</td>';
echo '<td>'.$row->BILL.'</td>';
echo '<td>'.$actual_paid.'</td>';
echo '<td>'.$actual_bal.'</td>';
}
WRONG RESULT
| MONTH | BILL | AMT PAID | BALANCE |
-------------------------------------------------------
| January | 1,500 | 2,000 | -500 |
| February | 2,000 | 2,000 | 0 |
我想要显示确切的金额和余额。 请帮忙,我希望你能猜出我的意思:D
答案 0 :(得分:0)
- 您的表格中应该有monthIndex或datetime
select t0.MONTHNAME, t0.bill, t0.pay, (select sum(bill - pay) from billing t1 where t1.MONTHINDEX <= t0.MONTHINDEX) bal
from billing t0