-2

what would be the mysql query to select the next value from the column

I have a column named postOrder with values 1 2 3 4 5 6 etc i have tried:

SELECT * FROM table WHERE such=such ORDER BY postOrder ASC LIMIT 1

but sometimes i get the wrong row, like 4 instead of 3 I think i can remove the LIMIT 1 clause, but then I would have to do more work with php.

Any ideas how to make sure I always get the next row without having to get all the rows and then sort them with php? thanks.

Mohammad hayajneh
  • 623
  • 2
  • 11
  • 32
Sam Arcos
  • 17
  • 1
  • 8

3 Answers3

0

If I understood your question correctly, I think the solution would be to use OFFSET like this:

SELECT * FROM table WHERE such=such ORDER BY postOrder ASC LIMIT 1 OFFSET 2;

With that offset you can get the correct value and do some pagination.

StefanR
  • 676
  • 10
  • 20
0

You can use offset to fetch next row(s):

select *
from table
order by postOrder
limit 1, 1;
------^----  Offset
---------^-- rows to fetch

or other syntax:

select *
from table
order by postOrder
limit 1 offset 1;

Take a look at this: https://www.w3schools.com/php/php_mysql_select_limit.asp

or this SO answer: Which rows are returned when using LIMIT with OFFSET in MySQL?

Gurwinder Singh
  • 38,557
  • 6
  • 51
  • 76
0

you can fetch the last value in the postOrder with a query, then add it to the main query conditions. something like this:

SELECT * FROM table
WHERE such = such 
AND postOrder IN (SELECT MAX(postOrder) FROM table)
Samer Abu Gahgah
  • 751
  • 1
  • 9
  • 18