How to get the last record entered in SQL according to your date

7

I need to get a record of a table, where this record is what the date was entered last.

Of the genre:

SELECT Ped.Quantidade
FROM ped
WHERE data <= @data

But as I have several records it returns me all, when I just want the last one inserted by the date.

    
asked by anonymous 16.04.2014 / 13:44

1 answer

11

Select the last record by sorting by decreasing date and restricting the number of lines with limit or top

SELECT Ped.Quantidade
FROM ped
WHERE data <=@data
ORDER BY data DESC limit 1

or

SELECT TOP 1 Ped.Quantidade
FROM ped
WHERE data <=@data
ORDER BY data DESC
    
16.04.2014 / 13:46