Bring a field, by MAX (ID)

0

How do I bring up any field, by the largest ID of the table. I made a MAX (Field1), Field2 and had to do a grouping by Field2 and the result it brought me several Field2 and not just one that has the largest ID.

Select MAX(ID), campo2 from tabela where campo3 = valor group by campo2

This did not work, it brought me several field2

    
asked by anonymous 09.02.2015 / 14:46

2 answers

2

If you want to simply bring the largest Id of the table, this only solves:

Select MAX(ID) from tabela;

After all, Id is unique and does not need to group values.

If you need to bring another field based on MAX(ID) , the correct one is:

select campo2
from tabela
where ID = (select MAX(ID) from tabela);
    
09.02.2015 / 14:48
0

So it worked, but I had to put a subquery in the game

select campo2 from tabela where campo3= 49 and campo1  in (select max(campo1) from tabela where campo3 = 49)

That's it.

    
09.02.2015 / 14:58