I'll-Be-Back
I'll-Be-Back

Reputation: 10828

How to get previous row data?

I want to find out what is the previous ID, how can that be done?

Find ID 5:

SELECT id FROM status` WHERE id = 5 ORDER BY order_status ASC

Now I want get previous ID depending on ORDER order_status ASC

Upvotes: 1

Views: 236

Answers (1)

Rene Pot
Rene Pot

Reputation: 24815

Use the MAX() function in combination with a WHERE like this:

SELECT MAX(id) FROM status WHERE id < 5

Note the ORDER BY has no use as you are only selecting one row

Upvotes: 4

Related Questions