MySQL实现根据当前ID读取上一条和下一条记录

时间:2020-08-03 23:34:06   收藏:0   阅读:134

当前ID为5,读取上一条记录:

    SELECT * FROM A WHERE ID < 5 ORDER BY ID DESC LIMIT 0,1;  

ps: select * from table1 where id<$id  order by id desc limit 1
 

当前ID为5,读取下一条记录:

    SELECT * FROM B WHERE ID > 5 ORDER BY ID LIMIT 0,1;  

  ps: select * from table1 where id>$id  order by id asc limit 1


查询前n行记录       select * from table1 limit 0,n;      或   select * from table1 limit n;

 

查询第一行记录:  select   *   from   table  limit   1

 

查询第n行到第m行记录:     select * from table1  limit n-1,m-n;

 

SELECT * FROM table LIMIT 5,10;返回第6行到第15行的记录

 

select * from employee limit 3,1; // 返回第4行

 
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!