I have a table data with following columns:
|-a-|-b-|-text-|
|-1-|-1-|-text-|
|-1-|-2-|-text-|
|-1-|-3-|-text-|
|-2-|-1-|-text-|
|-2-|-2-|-text-|
|-2-|-3-|-text-|
|-2-|-4-|-text-|
e.g. I am on 3rd record ($current – in bold), I need to make sql query to move to (2, 1).
I have the query as below:
$previous = "SELECT a, b FROM `table` WHERE a <= $current_a AND b < $current_b ORDER BY a DESC, b DESC LIMIT 1";
$next = "SELECT a, b FROM `table` WHERE a >= $current_a AND b > $current_b ORDER BY a ASC, b ASC LIMIT 1";
Though however next goes to (2, 4) instead.
Any ideas?
You need a condition that handles the two cases separately; when the a values are the same, and when they are different:
You need that for the previous item also: