我在分解给定付款金额时遇到问题,我认为我的代码中缺少某些内容。这是我的下表。
如果我支付了5,000,则它将在第一行扣除,剩余的金额将发送到第二行。
//这是付款
$payment = 5000;
$temp = '';
//功能循环
$data = payment_breakdown();
foreach ($data as $row) {
$amount = $row['AMOUNT'] - $payment."\n";
}
//处理函数每个循环的差异
$temp = $amount;
//金额结果
echo $amount;
//这是我从表中获取数据的功能
function payment_breakdown(){
$db = database2();
$query = "SELECT AMOUNT FROM TABLE1";
$cmd = $db->prepare($query);
$cmd->execute();
$rows = $cmd->fetchAll();
$db = null;
return $rows;
}
假设表上的金额是这个
Data From Table | The Payment
3000 | 3000
3000 | 2000
答案 0 :(得分:1)
您可以在SQL中完成此操作
SELECT
amount,
paid,
sum(amount-paid) open
FROM
payment
GROUP BY amount, paid
WITH ROLLUP
HAVING
amount IS NULL
OR paid IS NOT NULL
;
基于数据
INSERT INTO payment (amount, paid)
VALUES
(3000,3000),
(3000,2000),
(4000,1000)
;
你得到
amount;paid; open
3000 ;2000; 1000
3000 ;3000; 0
4000 ;1000; 3000
NULL ;NULL; 4000
或者您可能希望简单地从存款中扣除:
SET @credit := 5000;
SELECT
amount,
@credit := @credit-amount credit
FROM payment
;
SELECT @credit;
但是,在后一种情况下,您应该宁愿将所有付款和要付款的金额存储到数据库表中,然后将它们总计在一起。考虑重构您的数据库结构设计。