How to recover the last payment? [duplicate]

2

I have the following table in my bank:

"financeiro
    financeiro_id INT AUTO_INCREMENT,
    financeiro_pagamento DATE,
    financeiro_pid INT,
    financeiro_valor INT,
";

In this table I record the payments that my system receives, then I use the following Query to return the values:

"SELECT *FROM login lo 
  inner join financeiro fi on lo.login_id = fi.financeiro_pid 
  Order by financeiro_pagamento";

This Query takes the financeiro_pid and joins it with the login table to know who paid, but I need every record equal to that table to return only the person's last payment. With this Query I'm getting all the payments but I just wanted the last payment from the user.

Note: financeiro_pid is where I register the user key.

    
asked by anonymous 02.11.2017 / 18:03

2 answers

1

The query below should do what you want. You will only get the most recent payments from each client ( MAX() ):

SELECT * FROM financeiro fi 
   inner join login lo on lo.login_id = fi.financeiro_pid
WHERE
   fi.financeiro_pagamento = (SELECT MAX(fi2.financeiro_pagamento) FROM financeiro fi2 where fi2.financeiro_pid = fi.financeiro_pid)
ORDER BY
   financeiro_pagamento
    
02.11.2017 / 20:32
0

Do not use max()

Uses last_insert_id() :

Reason:

link

Yes, there is some relationship between max id and most recent insert, but consider the following:

What if the most recently inserted row was deleted?

    
03.11.2017 / 00:00