Select the line with the largest ID [duplicate]

1

I have the val_products table, where product id and product_value , I need to give a 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 in that select only the line with ID 2. I tried using max (product_id) , however it gives error if used after where.

    
asked by anonymous 03.01.2018 / 20:02

3 answers

6

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

select * from val_produtos order by id desc limit 1
    
03.01.2018 / 20:11
3

One way to do this is

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

This will get the value with the highest id

    
03.01.2018 / 20:10
1

Try to do this:

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

    
03.01.2018 / 20:05