user3781174
user3781174

Reputation: 235

Select previous record in sqlite

I have this syntax sql who catches the record whose id is equal to what I set:

SELECT * FROM table one WHERE id = '128'

So I select the previous record by id I could just take that number and subtract by 1 (128-1 = 127), but we assume that the record 127 is excluded from the table? In this case he would select an id that does not exist.

How I can change my syntax sql to select previous record?

Upvotes: 0

Views: 167

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

You can do it like this:

SELECT *
FROM table one
WHERE id < 128
ORDER BY id DESC
LIMIT 1;

Or, if you just want the id:

SELECT MAX(id)
FROM table one
WHERE id < 128;

Upvotes: 2

Related Questions