开发者

Simplest way to get the next Row from an SQL table when I know the second one

Hi everyone I am trying to write a sorting script

For this my user will click a move up button which posts the id of the current selection we want to move up to a new page where the script is processed.

开发者_如何学JAVASo using the fetch functions below i am getting the sort id of the current row we want to move up

$sqlCurrent = "SELECT * FROM `contact` WHERE `contact_id` = $id LIMIT 0, 1 ";

$resultsCurrent= mysql_query($sqlCurrent) or die(mysql_error());

$rowC = mysql_fetch_row($resultsCurrent);

$currentSort =$rowC[9];

I then pulled out all the data in decending order using

Now if my current sort order is 6 I want to look for the row with the sort order with 3 or 4 or 5 in order so i used the decending order and the next sort scrip comes up in the next table.

$sql = "SELECT * FROM `contact` ORDER BY `contact`.`contact_sortOrder` DESC LIMIT 0, 30 ";

The question is how do I simply get data from that row using 1 or maybe 2 functions.

We cant simply look for the next sort order because it is possible it wont be there.

For this example i have used a database like this

rowId 1 Sort order 6
rowId 2 Sort Order 2
rowId 3 Sort Order 4

Now I am row Id 3 and want to replace it with the next one. So i need to pick up rowId 2 somehow using the shortest method.

Any help will be useful


Could be as simple as

$query = "
  SELECT
    x,y,z
  FROM
    contact
  WHERE
    contact_sortOrder < $currentSort
  LIMIT
    1
";

(assuming $currentSort is safe for "direct insertion" into the query. see http://docs.php.net/pdo.prepared-statements)


select      prev.*
from        contact as curr
left join   contact as prev on prev.contact_sortOrder < curr.contact_sortOrder
where       curr.contact_id = $id
order by    prev.contact_sortOrder desc
limit 1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜