如何获取表中现在行的下一行和上一行

时间:2014-01-14 18:41:48

标签: mysql sql

STRUCTURE TABLES AND ERROR WHEN EXECUTE QUERY ON SQLFIDDLE

我有表News

例如,我得到NewsIdn = '164955'

的行
SELECT * FROM News WHERE NewsIdn = '164955'

上一行有NewsIdn = '270085',下一行有NewsIdn = '324955'

请告诉我如何使用mysql查询获取上一个NewsIdn和下一个NewsIdn

2 个答案:

答案 0 :(得分:2)

也许是这样的:

主要查询:

SELECT * FROM News WHERE NewsIdn = '164955';

上一行:

select * from News WHERE ID < 
    (select ID from News where NewsIdn = '164955') Order by ID DESC Limit 1;

下一行:

select * from News WHERE ID > 
    (select ID from News where NewsIdn = '164955') Order by ID Limit 1;

答案 1 :(得分:0)

试试这个:

一个:

SELECT * FROM News WHERE NewsIdn < '164955' ORDER BY NewsIdn DESC LIMIT 1

下一步:

SELECT * FROM News WHERE NewsIdn > '164955' ORDER BY NewsIdn LIMIT 1