1. 程式人生 > >數據庫復習

數據庫復習

class not pos tween 函數 row from 大致 where

一.SQL Server查詢第31到40條數據?

大致分為兩種情況:ID連續和ID不連續。

1.ID連續的情況:select * from A where ID between 31 and 40
2.ID不連續的情況:

(1)兩次對表查詢,效率較低。
  select top 10 * from A where ID not in (select top 30 ID from A)
(2)外層查詢沒有對表A進行查詢,效率提高。
  select top 10 * from (select top 40 ID from A order by ID) as a order by a.ID desc
(3)ROW_NUMBER()函數效率更高,SQL2005以上版本可用。
  select * from(select *,ROW_NUMBER() over(order by ID)as ‘userID‘ from A) as a where a.userID between 31 and 40

數據庫復習