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

当前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行

 
posted @ 2020-08-03 22:51  RunningNeverStop  阅读(290)  评论(0编辑  收藏  举报