資料庫學習 mysql 分頁查詢語句

2021-06-25 07:43:13 字數 1304 閱讀 9605



單條 sql 語句的分頁 sql

方法1:

適用於 sql server 2000/2005

select top 頁大小 * from table1 where id not in ( select top 頁大小*(頁數-1) id from table1 order by id ) order by id

方法2:

適用於 sql server 2000/2005

select top 頁大小 * from table1 where id > ( select isnull(max(id),0) from ( select top 頁大小*(頁數-1) id from table1 order by id ) a ) order by id

方法3:

適用於 sql server 2005

select top 頁大小 * from ( select row_number() over (order by id) as rownumber,* from table1 ) a where rownumber > 頁大小*(頁數-1)

說明,頁大小:每頁的行數;頁數:第幾頁。使用時,請把「頁大小」以及「頁大小*(頁數-1)」替換成數碼。

mysql

select * from tt limit 1,20

select * from tt limit 21,30

/*如果你是幾千上萬資料,就直接使用mysql自帶的函式 limit的普通用法就ok了,如果是100萬以上的資料,可能就要講方法了,下面我們來做個百萬級資料的分頁查詢語句.

mysql> select * from news where id>=(select id from news limit 490000,1) limit 10;    sec  //很 明顯,這 種方式勝出 .

mysql> select * from news limit 490000,10  sec;

*/以下的文章主要介紹的是mysql分頁的實際操作方案,其實關於實現mysql分頁的最簡單的方法就是利用利用mysql資料庫的limit函式,limit [offset,] rows可以從mysql資料庫表中第m條記錄開始檢索n條記錄的語句為:

select * from 表名稱 limit m,n 

例如從表sys_option(主鍵為sys_id)中從第10條記錄開始檢索20條記錄,語句如下:

select * from sys_option limit 10,20  

select * from table [查詢條件] order by id limit ?,?  

oracle

MySQL 資料庫 分頁查詢

在使用mysql 進行資料庫分頁查詢的時候最主要是使用limit子句進行查詢 limit子句可以用來限制由select語句返回過來的資料數量,它有乙個或兩個引數,如果給出兩個引數,第乙個引數指定返回的第一行在所有資料中的位置,從0開始 注意不是1 第二個引數指定最多返回行數。例如 select fr...

MySQL 資料庫 分頁查詢

在使用mysql 進行資料庫分頁查詢的時候最主要是使用limit子句進行查詢 limit子句可以用來限制由select語句返回過來的資料數量,它有乙個或兩個引數,如果給出兩個引數,第乙個引數指定返回的第一行在所有資料中的位置,從0開始 注意不是1 第二個引數指定最多返回行數。例如 select fr...

資料庫MySQL之分頁查詢

當我們在京東購物,瀏覽商品列表的時候,由於資料特別多,一頁顯示不完,一頁一頁的進行顯示,這就是分頁查詢 select from 表名 limit start,count說明 limit是分頁查詢關鍵字 start表示開始行索引,預設是0 count表示查詢條數 例1 查詢前3行男生資訊 select...