Home >Database >Mysql Tutorial >用一条SQL语句取出第 m 条到第 n 条记录的方法

用一条SQL语句取出第 m 条到第 n 条记录的方法

WBOY
WBOYOriginal
2016-06-07 14:54:451152browse

用一条SQL语句取出第 m 条到第 n 条记录的方法 取出 记录 --从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本) SELECT TOP n-m+1 * FROM Table WHERE (id NOT IN (SELECT TOP m-1 id FROM Table )) --从TABLE表中取出第m到n条记录 (Exists版本) SELECT

用一条SQL语句取出第 m 条到第 n 条记录的方法

取出 记录
 --从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本)
  
  SELECT TOP n-m+1 * 
  FROM Table 
  WHERE (id NOT IN (SELECT TOP m-1 id FROM Table ))  
  
  --从TABLE表中取出第m到n条记录 (Exists版本)
  
  SELECT TOP n-m+1 * FROM TABLE AS a WHERE Not Exists
  (Select * From (Select Top m-1 * From TABLE order by id) b Where b.id=a.id ) 
  Order by id
 
 
  --m为上标,n为下标,例如取出第8到12条记录,m=8,n=12,Table为表名,Temp为临时表
 
  Select Top n-m+1 * From Table 
  Where Id>(Select Max(Id) From 
  (Select Top m-1 Id From Table Order By Id Asc) Temp) 
  Order By Id Asc

  --找出升序的第m到第n条记录,更为笨拙的办法是

  Select * From 
  (Select Top n-m+1 * From (Select Top n * From 表名 Order By id Desc) t1 Order By id) t2 
  Order By id 
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:SQL导出为Excel表Next article:枚举SQL中的Agent代理