badbye badbye - 4 months ago 9
SQL Question

MySQL: How to select rows which has next lager ID?

Suppose I have a table as such:

ID Content

3 blah

5 blah <--

6 blah

7 blah


I want to get the row with id number next higher than 4 (which would be 5)

I tired

SELECT MIN (DISTINCT id)
FROM mytablename
WHERE id > 4

Answer

Use LIMIT:

SELECT *
FROM mytablename
WHERE id > 4
ORDER BY id
LIMIT 1

Demo Here