Select from the line with the highest ID

Asked

Viewed 2,770 times

1

I have the val_products table, where you have id_product and product value, I need to select * only on the line whose ID is the largest, for example, I have two products with ID’s 1 and 2, but I need to return on this select only the line with ID 2. I tried to use the max(id_product), but it gives error if used after Where.

3 answers

6


One option, not to use sub queries, could be to sort down by ID and return only the record most recent

select * from val_produtos order by id desc limit 1

3

One way to do it is this

Select * from val_produtos order by id DESC LIMIT 0, 1

This way you will get the value with the highest id

1

Try to do so:

Select * from val_produtos where id_produto = (select max(id_produto) from val_produtos)

A subselect that will return you the id of the largest

  • 1

    It is not necessary to make a subselect, the ideal is to sort by the ID field and limit the query to 1 result.

Browser other questions tagged

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