zoukankan      html  css  js  c++  java
  • 用一句SQL取出第 m 条到第 n 条记录的方法

    1 --从Table 表中取出第 m 条到第 n 条的记录:(Not In 版本)
     2 
     3 Select TOP n-m+1 * 
     4 FROM Table 
     5 Where (id NOT IN (Select TOP m-1 id FROM Table ))  
     6 
     7 
     8 --从TABLE表中取出第m到n条记录 (Exists版本)
     9 
    10 Select TOP n-m+1 * FROM TABLE AS a Where Not Exists
    11 (Select * From (Select Top m-1 * From TABLE order by id) b Where b.id=a.id ) 
    12 order by id
    13 
    14 
    15 --m为上标,n为下标,例如取出第8到12条记录,m=8,n=12,Table为表名
    16 
    17 Select Top n-m+1 * From Table 
    18 Where Id>(Select Max(Id) From 
    19 (Select Top m-1 Id From Table order By Id Asc) Temp) 
    20 order By Id Asc   
     
    ============================
    #  Romance2010 发表于2007-02-27 11:40:38  IP: 
    从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


    不要order by id為正解,因為先order by 再top,如:
    Select TOP n-m+1 * FROM TABLE AS a Where Not Exists
    (Select * From (Select Top m-1 * From TABLE ) b Where b.id=a.id ) 
    order by id
  • 相关阅读:
    learning scala view collection
    scala
    learning scala dependency injection
    learning scala implicit class
    learning scala type alise
    learning scala PartialFunction
    learning scala Function Recursive Tail Call
    learning scala Function Composition andThen
    System.Threading.Interlocked.CompareChange使用
    System.Threading.Monitor的使用
  • 原文地址:https://www.cnblogs.com/lds85930/p/1507771.html
Copyright © 2011-2022 走看看