douguai7291 2015-11-05 08:39
浏览 39

使用PHP SQL从账单中扣除金额

Hi Guys I have been coding and can't have a good solution to this problem... Sample: From BILLING table there is 2 bills that is cost for January 1,500 and February 2,000 From PAYMENT table the client paid 2,000 and this amount should deducting to the Jan BILL and Feb Bill since 2,000 is greater than the amount of January so should be automatically deducting the 500 to the next BILL February or so on. Table

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    |

I want to show a exact amount and balance. please help, I hope you guess what I mean to say :D

  • 写回答

1条回答 默认 最新

  • duanjiao8871 2015-11-05 09:03
    关注

    -- You should have monthIndex or datetime in your table

    select t0.MONTHNAME, t0.bill, t0.pay, (select sum(bill - pay) from billing t1 where t1.MONTHINDEX <= t0.MONTHINDEX) bal
    from billing t0
    
    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误