Dennis Volodomanov <info-+Tq6r2yh00lWk0Htik3J/[EMAIL PROTECTED]> wrote:
Igor Tandetnik wrote:
Dennis Volodomanov <info-+Tq6r2yh00lWk0Htik3J/[EMAIL PROTECTED]>
wrote:
Let's say I have a simple schema:

CREATE TABLE MyTable ( ID INTEGER PRIMARY KEY, SomeData )

What I'd like to get is 5 records (for example) that are immediately
before a given ID (where before means their ID is less than).

select * from MyTable
where ID < ?
order by ID desc limit 5;

Ah, thank you! I had a similar one but was doing an "asc" to get the
order I wanted, which doesn't make sense - I should sort the results
after I get them on my own.

You could do something like this:

select * from MyTable
where ID in (
   select ID from MyTable
   where ID < ?
   order by ID desc limit 5
)
order by ID asc;  -- or whatever order you want

Igor Tandetnik

-----------------------------------------------------------------------------
To unsubscribe, send email to [EMAIL PROTECTED]
-----------------------------------------------------------------------------

Reply via email to