php - 返回MySQL中相同id组的最后一个插入行

标签 php mysql

我正在使用创建付款收据,我将支付一些金额,如下例所示

此收据已创建,但仍需支付 due_amount

cust_id   cust_name  invoice_id  paid_amount  due amount  makeapayment

1001       SAS              545      76           305          sasi 

这是我查询的结果。
我必须支付到期金额 100,然后在支付相同的“id”后创建另一行查看新行旧行不查看仅留在数据库中查看像这样的结果

cust_id   cust_name  invoice_id  paid_amount  due amount  makeapayment

1001       SAS              545       76           205          sasi   

目前我正在使用查询

SELECT pid, cust_id, cust_name, date, invoice_id, invoice_date, invoice_amount, paid_amount, paymentmade, makeapayment, due_amount, status FROM print_reciept
group by cust_id having due_amount!='0' ORDER BY pid DESC          

最佳答案

我想你可以试试这个

SELECT pid, cust_id, cust_name, date, invoice_id, invoice_date, invoice_amount, paid_amount, paymentmade, makeapayment, due_amount, status 
FROM print_reciept AS pr
INNER JOIN (
    SELECT MAX(pid) AS last_pid, cust_id
    FROM print_reciept
    GROUP BY cust_id
) AS tmp ON (tmp.cust_id = pr.cust_id AND tmp.last_pid > pr.pid)
GROUP BY cust_id 
ORDER BY pid DESC

关于php - 返回MySQL中相同id组的最后一个插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31900914/

相关文章:

php - Mysql SELECT函数在字段列表中返回未知列错误

mysql - 找不到 mysql.sock

mysql - mysql : com. mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 中出现错误:表 'db.studentgroup' 不存在

PHP 相当于 javascript 中的 Date.UTC 函数?

PHP Firebase Clood Messaging (FCM) 错误缺少注册

php - 将 PostgreSQL 连接库添加到 Azure 应用服务中

php - mysql。以月为单位的日期差异

mysql - 如何在MySQL中添加选择时不存在的行

php - 简单的PHP/MySQL表单仅刷新

php - Outlook 2007 接收带有标题的 html 邮件作为源,其他 MUA 工作正常。为什么?