next lowest number in mysql
how do i do a query that i want the next lowest number higher then a certain number
so for example if i have data like in the id step_number
1
3
4
5
6
if i just want the next highest number higher then 1...but i dont know the numbers they vary every time. I just know i need the next lowest number higher then a number. So in t开发者_开发技巧his case i would want 3 returned
select step_number from system_step_product where step_number > 1
order by step_number asc limit 1;
this works but is there a better way
Your solution might be the most performant, but another solution that would work in other DB's would be:
SELECT MIN(step_number)
FROM system_step_product
WHERE step_number > 1
Indexing that column is also probably a good idea.
I don't think there's a better way; that's a pretty efficient way of doing it.
Also, do you have a reason for looking for a better way? To quote Knuth,
"Premature optimization is the root of all evil."
This also works:
SELECT MIN(step_number)
FROM system_step_product
WHERE step_number > 1
精彩评论