mysql 分页使用 limit关键字,limit x,y (x代表从哪条数据开始,y代表页面大小。mysql第一条数据在limit计算时索引为0)
limit 10
  前10条
limit 0,10
  从第1条开始的10条
limit 10,10
  从第 11 条开始的 10 条
limit 100,10
 从第101条开始的10条
 数据量大时(>千万),效率低
oracal 分页,使用 oracle的特殊列 rownum
select * from
(select *,rownum R from
(select * from a)

实例:查找入职员工时间排名倒数第三的员工所有信息。limit 2,1代表选择从第3条数据开始的1条数据,即第3页数据,页面大小为1

select * from employees order by hire_date desc limit 2,1

前n条数据,即从索引0开始计算:limit n 或者 limit 0,n

mysql> select * from employees order by hire_date desc limit 5;
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| employee_id | first_name | last_name | email | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| 173 | Sundita | Kumar | SKUMAR | 011.44.1343.329268 | 2000-04-21 | SA_REP | 6100.00 | 0.10 | 148 | 80 |
| 167 | Amit | Banda | ABANDA | 011.44.1346.729268 | 2000-04-21 | SA_REP | 6200.00 | 0.10 | 147 | 80 |
| 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 2000-03-24 | SA_REP | 6400.00 | 0.10 | 147 | 80 |
| 128 | Steven | Markle | SMARKLE | 650.124.1434 | 2000-03-08 | ST_CLERK | 2200.00 | NULL | 120 | 50 |
| 165 | David | Lee | DLEE | 011.44.1346.529268 | 2000-02-23 | SA_REP | 6800.00 | 0.10 | 147 | 80 |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
5 rows in set (0.00 sec) mysql> select * from employees order by hire_date desc limit 0,5;
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| employee_id | first_name | last_name | email | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| 173 | Sundita | Kumar | SKUMAR | 011.44.1343.329268 | 2000-04-21 | SA_REP | 6100.00 | 0.10 | 148 | 80 |
| 167 | Amit | Banda | ABANDA | 011.44.1346.729268 | 2000-04-21 | SA_REP | 6200.00 | 0.10 | 147 | 80 |
| 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 2000-03-24 | SA_REP | 6400.00 | 0.10 | 147 | 80 |
| 128 | Steven | Markle | SMARKLE | 650.124.1434 | 2000-03-08 | ST_CLERK | 2200.00 | NULL | 120 | 50 |
| 165 | David | Lee | DLEE | 011.44.1346.529268 | 2000-02-23 | SA_REP | 6800.00 | 0.10 | 147 | 80 |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
5 rows in set (0.00 sec)

更多例子

limit 5     前5条数据
limit 2,1   第3页数据,页面大小为1
limit 2,2   第2页数据,页面大小为2
limit 2,3   第3条数据起,共3条数据,此数据不符合分页数据显示格式
分页显示格式:limit startIndex pageSize
startIndex = (需要查询的页码数 - 1) * pageSize
mysql> select * from employees order by hire_date desc limit 5;
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| employee_id | first_name | last_name | email | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| 173 | Sundita | Kumar | SKUMAR | 011.44.1343.329268 | 2000-04-21 | SA_REP | 6100.00 | 0.10 | 148 | 80 |
| 167 | Amit | Banda | ABANDA | 011.44.1346.729268 | 2000-04-21 | SA_REP | 6200.00 | 0.10 | 147 | 80 |
| 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 2000-03-24 | SA_REP | 6400.00 | 0.10 | 147 | 80 |
| 128 | Steven | Markle | SMARKLE | 650.124.1434 | 2000-03-08 | ST_CLERK | 2200.00 | NULL | 120 | 50 |
| 165 | David | Lee | DLEE | 011.44.1346.529268 | 2000-02-23 | SA_REP | 6800.00 | 0.10 | 147 | 80 |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
5 rows in set (0.00 sec) mysql> select * from employees order by hire_date desc limit 2,1;
+-------------+------------+-----------+-------+--------------------+------------+--------+---------+----------------+------------+---------------+
| employee_id | first_name | last_name | email | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id |
+-------------+------------+-----------+-------+--------------------+------------+--------+---------+----------------+------------+---------------+
| 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 2000-03-24 | SA_REP | 6400.00 | 0.10 | 147 | 80 |
+-------------+------------+-----------+-------+--------------------+------------+--------+---------+----------------+------------+---------------+
1 row in set (0.00 sec) mysql> select * from employees order by hire_date desc limit 2,2;
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| employee_id | first_name | last_name | email | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 2000-03-24 | SA_REP | 6400.00 | 0.10 | 147 | 80 |
| 128 | Steven | Markle | SMARKLE | 650.124.1434 | 2000-03-08 | ST_CLERK | 2200.00 | NULL | 120 | 50 |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
2 rows in set (0.00 sec) mysql> select * from employees order by hire_date desc limit 2,3;
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| employee_id | first_name | last_name | email | phone_number | hire_date | job_id | salary | commission_pct | manager_id | department_id |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
| 166 | Sundar | Ande | SANDE | 011.44.1346.629268 | 2000-03-24 | SA_REP | 6400.00 | 0.10 | 147 | 80 |
| 128 | Steven | Markle | SMARKLE | 650.124.1434 | 2000-03-08 | ST_CLERK | 2200.00 | NULL | 120 | 50 |
| 165 | David | Lee | DLEE | 011.44.1346.529268 | 2000-02-23 | SA_REP | 6800.00 | 0.10 | 147 | 80 |
+-------------+------------+-----------+---------+--------------------+------------+----------+---------+----------------+------------+---------------+
3 rows in set (0.00 sec)

mysql 使用技巧 分页limit的更多相关文章

  1. MYSQL分页limit速度太慢优化方法

    http://www.fienda.com/archives/110 在mysql中limit可以实现快速分页,但是如果数据到了几百万时我们的limit必须优化才能有效的合理的实现分页了,否则可能卡死 ...

  2. MySQL分页limit速度太慢的优化方法

    limit用法 在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. SELECT * FROM table LIMIT ...

  3. 如何优化Mysql千万级快速分页,limit优化快速分页,MySQL处理千万级数据查询的优化方案

    如何优化Mysql千万级快速分页,limit优化快速分页,MySQL处理千万级数据查询的优化方案

  4. mysql的sql分页函数limit使用 (转)

    http://www.cnblogs.com/beijingstruggle/p/5631603.html mysql的sql分页函数limit使用 My sql数据库最简单,是利用mysql的LIM ...

  5. 第二百八十八节,MySQL数据库-索引、limit分页、执行计划、慢日志查询

    MySQL数据库-索引.limit分页.执行计划.慢日志查询 索引,是数据库中专门用于帮助用户快速查询数据的一种数据结构.类似于字典中的目录,查找字典内容时可以根据目录查找到数据的存放位置,然后直接获 ...

  6. MySQL 百万级分页优化

    MySQL 百万级分页优化 http://www.jb51.net/article/31868.htm 一般刚开始学SQL的时候,会这样写 : , ; 但在数据达到百万级的时候,这样写会慢死 : , ...

  7. [数据库]Oracle和mysql中的分页总结

    Mysql中的分页 物理分页 •在sql查询时,从数据库只检索分页需要的数据 •通常不同的数据库有着不同的物理分页语句 •mysql物理分页,采用limit关键字 •例如:检索11-20条 selec ...

  8. jquery ajax php+mysql 无刷新分页 详细实例

    最近在接触jquery和ajax,当前项目也会用到分页,为了用户体验更好一些,就准备用无刷新分页,这个demo很适合新手学习查看,写的比较清晰,话不多说,直接上代码吧. 首先是html页面,index ...

  9. 分页查询信息(使用jdbc连接mysql数据库实现分页查询任务)

             分页查询信息       使用jdbc连接mysql数据库实现分页查询任务 通过mysql数据库提供的分页机制,实现商品信息的分页查询功能,将查询到的信息显示到jsp页面上. 本项目 ...

随机推荐

  1. window 查看端口 杀端口

    最近写项目,总是出现端口被占用的问题,原来傻傻的把电脑重启一下,终于有一天受不了了,想要想办法解决.刚开始从网上找了好多教程,发现不行.开始自己尝试,终于,成功的将占用端口的进程杀掉.在此记录下过程( ...

  2. 基于 websocket 的多端桥接平台

    我们现在的业务是基于新闻客户端实现的,都要经过新闻客户端的环境,进行前后端数据上的交互.但是我们在调试过程中,非常的不方便. 通常使用的工具有:modheader, postman, fiddler ...

  3. Jupyter的安装和基本使用

    1. 安装Jupyter pip install jupyter 2. Jupyter的初次使用 # 进入虚拟环境 workon ai # 输入命令 jupyter notebook 本地notebo ...

  4. Redis底层函数详解

    Redis底层函数详解 serverCron 函数 它负责管理服务器的资源,并维持服务器的正常运行.在执行 serverCron 函数的过程中会调用相关的子函数,如 trackOperationsPe ...

  5. OpenLDAP 多主复制(基于docker容器模式部署)

    **本文主要讲述在docker环境下如何进行 OpenLDAP 多主复制,至于 OpenLDAP 原理可以先参考这篇文章了解:https://cloud.tencent.com/developer/a ...

  6. python ndarray与pandas series相互转换,ndarray与dataframe相互转换

    https://blog.csdn.net/qq_33873431/article/details/98077676

  7. nodejs使用express中静态资源托管(express.static())时遇到的bug

    如下:将test.html的页面挂载在服务器上, const express= require('express') const fs= require('fs') let app = express ...

  8. 使用electron和node-serialport的环境搭建过程

    项目运行所需环境 1,必须安装nodejs 附上node下载地址-Nodejs node安装过程简单, 一直next就行了,我安装的版本是12.16.1,可以在powershell中通过 node - ...

  9. ThreadAbortException是可以传递的

    今天在写线程Aborted代码时,发现嵌套的try catch中的ThreadAbortException错误是可以从内部传递到外部的,想想这也是必然的,在内部该线程已经中断了,外部必然是中断了,再仔 ...

  10. 一文搞懂 ThreadLocal 原理

    当多线程访问共享可变数据时,涉及到线程间同步的问题,并不是所有时候,都要用到共享数据,所以就需要线程封闭出场了. 数据都被封闭在各自的线程之中,就不需要同步,这种通过将数据封闭在线程中而避免使用同步的 ...