Select MAX(ID) does not work correctly

Asked

Viewed 57 times

0

Can anyone tell me why this SELECT down in the MYSQL is getting the biggest ID correctly, but the fields (value, date and status) do not bring the information of the largest ID returned. The SELECT is getting the biggest ID, but the information of the fields (valor, data and status) returned are from another ID.

SELECT max(id), valor, afiliado_id, data, status FROM afiliado_saque_logs WHERE afiliado_id = 27

And when I do the way down by putting the (afiliado_id = 27) concerning the person I want to bring the greatest ID with the fields (valor, data, status) there returns nothing, and there is information for the afiliado_id = 27.

SELECT id, valor, afiliado_id, data, statusFROM afiliado_saque_logsWHERE id = (SELECT MAX(id) FROM afiliado_saque_logs) and afiliado_id = 27

1 answer

1

Query 1:

This is happening because you are looking for affiliate_id 27. Its query brings the largest id that exists in the database, but the data it retrieves from user 27.

Query 2:

Your query is looking for the line where the id is the largest number and the affiliate_id for 27. Not returning anything because the affiliate_id 27 probably doesn’t have the highest id.

If you want the first query to work correctly and return the data of the largest id found you can replace it with:

SELECT id, valor, afiliado_id, data, status FROM afiliado_saque_logs WHERE id = (SELECT MAX(id) FROM afiliado_saque_logs)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.