Skip to content
Advertisement

How can I get the 10 most recent rows from a database?

I want to choose an id in a table, sort it by date and show only the 10 newest entries.

I have already tried following command:

SELECT * FROM weather 
  WHERE DATUM = (SELECT MAX(DATUM) WHERE ID='0')

Advertisement

Answer

It looks like you want filtering, sorting and limiting:

select *
from weather
where id = 0          -- filter on the given "id"
order by datum desc   -- sort by most recent date
limit 10              -- keep the 10 most recent only
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement