SQL取m到n條資料 資料庫分頁

2021-05-28 00:00:27 字數 1890 閱讀 3506

/*取表中5--8條資料*/

select top 4 employeeid from dbo.employees where employeeid not in(

select top 4 employeeid from dbo.employees order  by employeeid asc)

select top 4 employeeid from

(select top 8 employeeid from dbo.employees order by employeeid asc) a

order by employeeid desc

--identity(int)是在臨時表上面新增一列用於標示原來的非連續的記錄

--這種情況是原來的表中沒有identity列

select identity(int) id0,* into #tmp1 from yo

select * from #tmp1 where id0>=5 and id0<=8

--identitycol是乙個關鍵字

select * from yo where identitycol between 5 and 8

--利用差集

select top 8 * from yo

except

select top 4 * from yo

--資料庫分頁

declare @pagesize int

declare @pageindex int

set @pagesize=4

set @pageindex=2

select top (@pagesize) yoid from yo where yoid not in

(select top (@pagesize*(@pageindex-1)) yoid from yo order by yoid)

order by yoid

declare @pagesize int

declare @pageindex int

set @pagesize=4

set @pageindex=2

select top (@pagesize) yoid from yo

where yoid >

(select isnull(max(yoid),0) from

(select top (@pagesize*(@pageindex-1)) yoid from yo order by yoid) a ) order by yoid

--08/05平台上使用;另乙個排序的是rank() over 是乙個跳躍排序

--這樣的程式在面對資料量大的情況下可以使用

declare @pagesize int

declare @pageindex int

set @pagesize=4

set @pageindex=2

select top (@pagesize) * from

(select row_number() over (order by yoid) as rownumber,* from yo) a

where rownumber>(@pagesize*(@pageindex-1))

declare @pagesize int

declare @pageindex int

set @pagesize=4

set @pageindex=2

select * from

(select row_number() over (order by yoid) as rownum,* from yo) as y

where rownum between  (@pagesize*(@pageindex-1))+1 and (@pagesize*@pageindex) 

hql分頁(獲取從第m到第n條資料)

hibernate封裝了sql語句成了hql,雖然對於多表級聯查詢顯得很麻煩甚至不如sql,但是其實用慣了的話,對於一些常規資料的查詢還是很方便的,比較自動給封裝到物件裡了,不像sql那般麻煩。那麼下面就解決一下hql分頁顯示問題,也就是只取結果集中某一段的結果,怎麼寫呢?其實很簡單,hql的que...

hql分頁(獲取從第m到第n條資料)

hibernate封裝了sql語句成了hql,雖然對於多表級聯查詢顯得很麻煩甚至不如sql,但是其實用慣了的話,對於一些常規資料的查詢還是很方便的,比較自動給封裝到物件裡了,不像sql那般麻煩。那麼下面就解決一下hql分頁顯示問題,也就是只取結果集中某一段的結果,怎麼寫呢?其實很簡單,hql的que...

mysql查詢第n到第m條資料

mysql如何指定查詢一張表的查詢結果,如最後5行記錄和最前5行記錄 我們以student表為例,裡面有三個字段 id,name,age,其中id為主健,為自增,裡面共有10條記錄,如下所示。mysql select from student id name age 1 li 11 2 zh 12 ...