Php 我怎样才能在第二张桌子上得到最新的付款日期?

Php 我怎样才能在第二张桌子上得到最新的付款日期?,php,mysql,mysqli,Php,Mysql,Mysqli,这是我当前的查询 $sql=“选择全名,金额, 总额(付款付款_金额)为总付款, 借款人金额-总额(付款付款金额)为总余额, date\u借款,借款人id 来自借款人 在借款人身份证=付款借款人身份证 按借款人分组id订单date\u借款DESC” 第一表 第二表; 我的桌子; 加入将为您提供所有付款记录,您只需要最后一个。进行子查询: $sql = "SELECT full_name, amount, SUM(payments.payment_amount) as totalPay

这是我当前的查询

$sql=“选择
全名
金额
, 总额(
付款
付款_金额
)为
总付款
借款人
金额
-总额(
付款
付款金额
)为
总余额
date\u借款
借款人
id
来自
借款人
借款人
身份证
=
付款
借款人身份证
按借款人分组
id
订单date\u借款DESC”


第一表

第二表;

我的桌子;

加入将为您提供所有付款记录,您只需要最后一个。进行子查询:

$sql = "SELECT 
   full_name, amount, SUM(payments.payment_amount) as totalPayment,
   borrowers.amount - SUM(payments.payment_amount) as totalBalance, 
   date_borrowed, borrowers.id,
   (SELECT payment_date FROM payments 
       WHERE borrowers.id=payments.borrower_id 
       ORDER BY payment_date DESC LIMIT 0,1) 
       AS last_payment
    FROM borrowers GROUP BY borrowers.id ORDER BY date_borrowed DESC";

非常感谢阿克塞尔。