1、查询第一行记录:

select  *  from  table1 limit  1;

2、查询第n行到第m行记录,或者第n行

   -- 2.1 查询连续的多行记录(第n~m行)
   select * from table1  limit n-1,m-n+1;

   -- 查询第6行到第15行的记录
   SELECT * FROM table1 LIMIT 5,10; 
   -----------------------------------------

   -- 2.2 查询第n行
   select * from table1 limit n-1,1;
   
   -- 查询第5行
   select * from employee limit 4,1; 
   
   -- 查询第10行
   select * from employee limit 9,1; 

3、查询前n行记录

 -- 方法一
 select * from table1 limit 0,n;
 -- 方法二
 select * from table1 limit n;

4、查询后n行记录

-- 倒序排序,取前n行 id为自增形式
select * from table1 order by id desc dlimit n;

5、查询一条记录($id)的下一条记录

select * from table1 where id>$id  order by id asc dlimit 1

6、查询一条记录($id)的上一条记录

select * from table1 where id<$id  order by id desc dlimit 1
Logo

更多推荐