栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据库系统原理与应用教程(044)—— MySQL 查询(六):使用 LIMIT 选项实现分页查询

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据库系统原理与应用教程(044)—— MySQL 查询(六):使用 LIMIT 选项实现分页查询

数据库系统原理与应用教程(044)—— MySQL 查询(六):使用 LIMIT 选项实现分页查询

目录
  • 数据库系统原理与应用教程(044)—— MySQL 查询(六):使用 LIMIT 选项实现分页查询
    • 一、LIMIT 选项的语法
    • 二、只指定一个参数
    • 三、指定两个参数
    • 四、使用 rows OFFSET offsets 格式
    • 五、利用 LIMIT 选项实现分页查询

在查询中使用 LIMIT 选项可以返回前几行或者中间某几行数据。

一、LIMIT 选项的语法

LIMIT 选项的语法格式如下:

LIMIT [offsets,] rows
-- 或者
LIMIT rows OFFSET offsets  


二、只指定一个参数

例如:查询课程 C102 成绩最高的 3 条记录。

mysql> select * from score where c_id = 'C102' 
       order by score desc limit 3;
+-------+------+-------+
| s_id  | c_id | score |
+-------+------+-------+
| S2013 | C102 |    97 |
| S2014 | C102 |    90 |
| S2022 | C102 |    88 |
+-------+------+-------+
3 rows in set (0.00 sec)
三、指定两个参数

例如:查询课程 C102 成绩 4-6 名的记录。

mysql> select * from score where c_id = 'C102' 
       order by score desc limit 3,3;
+-------+------+-------+
| s_id  | c_id | score |
+-------+------+-------+
| S2024 | C102 |    87 |
| S2011 | C102 |    84 |
| S2021 | C102 |    72 |
+-------+------+-------+
3 rows in set (0.00 sec)
四、使用 rows OFFSET offsets 格式

例如:查询课程 C102 成绩 4-6 名的记录。

mysql> select * from score where c_id = 'C102' 
    -> order by score desc limit 3 offset 3;
+-------+------+-------+
| s_id  | c_id | score |
+-------+------+-------+
| S2024 | C102 |    87 |
| S2011 | C102 |    84 |
| S2021 | C102 |    72 |
+-------+------+-------+
3 rows in set (0.00 sec)
五、利用 LIMIT 选项实现分页查询

(1)创建表 t11 并输入数据

mysql> create table t11(id int primary key auto_increment,name char(20),salary int);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t11(name, salary) values(concat('王',round(rand()*100000,0)),round(10000+10000*rand(),0));
Query OK, 1 row affected (0.04 sec)

--insert into t11(name,salary) select concat('王',round(rand()*100000,0)),round(10000+10000*rand(),0)) from t11;

mysql> select count(*) from t11;
+----------+
| count(*) |
+----------+
|     5632 |
+----------+
1 row in set (0.01 sec)

(2)每页显示 10 条记录

-- 第 1 页
mysql> select * from t11 limit 0,10;
+----+----------+--------+
| id | name     | salary |
+----+----------+--------+
|  1 | 王69892  |  19234 |
|  2 | 王52000  |  18300 |
|  3 | 王58978  |  14590 |
|  4 | 王52587  |  12522 |
|  5 | 王68343  |  16605 |
|  6 | 王25239  |  12803 |
|  7 | 王64457  |  13818 |
|  8 | 王97521  |  17307 |
|  9 | 王72786  |  14472 |
| 10 | 王5248   |  19208 |
+----+----------+--------+
10 rows in set (0.00 sec)

-- 第 2 页
mysql> select * from t11 limit 10,10;
+----+----------+--------+
| id | name     | salary |
+----+----------+--------+
| 11 | 王44640  |  14697 |
| 12 | 王928    |  16373 |
| 13 | 王15874  |  18818 |
| 14 | 王93259  |  10176 |
| 15 | 王29047  |  13994 |
| 16 | 王12568  |  14302 |
| 17 | 王77375  |  15783 |
| 18 | 王57015  |  11159 |
| 19 | 王86907  |  19976 |
| 20 | 王38096  |  19119 |
+----+----------+--------+
10 rows in set (0.00 sec)

-- 第 3 页
mysql> select * from t11 limit 20,10;
+----+----------+--------+
| id | name     | salary |
+----+----------+--------+
| 21 | 王41671  |  13478 |
| 22 | 王48877  |  14005 |
| 27 | 王53629  |  14799 |
| 28 | 王79065  |  15135 |
| 29 | 王19568  |  14378 |
| 30 | 王60202  |  16967 |
| 31 | 王67737  |  12968 |
| 32 | 王45187  |  13689 |
| 33 | 王48914  |  13389 |
| 34 | 王22692  |  11180 |
+----+----------+--------+
10 rows in set (0.01 sec)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023355.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号