使用 MYSql 从最大 table 值插入值

Insert values from max table value using MYSql

我 table 打电话给 CusPayment

PID     Date        No      TotalPaid
8523    05-Oct-14   GB0213  0.00
8523    08-Nov-14   GB0213  0.00
12659   28-Nov-14   N02007  0.00
12659   07-Dec-14   N02007  0.00
8523    07-Dec-14   GB0213  0.00
12659   04-Jan-15   N02007  0.00
8523    04-Jan-15   GB0213  0.00

我想在CardNo的基础上插入max TotalPaid,结果是

PID       Date      CardNo  TotalPaid
8523    07-Dec-14   GB0213  0.00
12659   04-Jan-15   N02007  0.00

你可以使用insert into select子句

如果同一张卡在不同日期有两个最大值,您将同时选择两者

insert into Table1 
select c.pid, c.date, c.cardno, c.TotalPaid 
from cusPayment c 
join ( select cardno, max(TotalPaid) as maxPaid 
       from CusPayment group by cardno ) t 
on c.cardno = t.cardno and c.TotalPaid = t.maxPaid