得到以前的& mysql查询的下一个id值

时间:2013-03-04 09:44:15

标签: php mysql

SELECT * FROM TableName WHERE name='test'

执行上述查询后,我想要上一个和下一个id值。当然,他们不会增量。如何根据where条件获取下一个和上一个id。

3 个答案:

答案 0 :(得分:1)

尝试以下

 select * from Tablename where name = 'test' AND id = (select min(id) from Tablename  where id > 4) OR id = (select max(id) from Tablename  where id < 4)

答案 1 :(得分:1)

SELECT
    * 
FROM
    TableName 
WHERE 
    id = (SELECT MIN(id) FROM TableName where id > 2) 
    AND id = (SELECT MAX(id) FROM TableName where id < 2)

您可以使用Limit

SELECT * FROM TableName WHERE `id` > 2 ORDER BY `id` ASC LIMIT 1
UNION 
SELECT * FROM TableName WHERE `id` < 2 ORDER BY `id` DESC LIMIT 1

答案 2 :(得分:0)

我相信如果您使用的是php

,可以使用mysql_data_seek()

http://php.net/manual/en/function.mysql-data-seek.php