MySQL全面瓦解9:查询的排序、分页相关
概述
数据库中的数据直接呈现出来一般不是我们想要的,所以我们上两节演示了如何对数据进行过滤的方法。除了对数据进行过滤,
我们可能还需要对数据进行排序,比如想从列表中了解消费最高的项,就可能需要对金额字段做降序排序,想看年龄从小到大的分布情况,就可能需要对user表的age字段进行升序排序。
也可能需要对数据进行限制,比如我们需要对付款的1~10,11~20,21~30 名的用户分别赠予不同的礼品,这时候对数据的限制就很有用了。
备注:下面脚本中[]包含的表示可选,| 分隔符表示可选其一。
数据排序 order by
语法格式如下:
1、需要排序的字段跟在order by
之后;
2、asc 和 desc表示排序的规则,asc:升序,desc:降序,默认为升序 asc;
3、排序可以指定多次字段,多字段排序之间用逗号隔开。
4、多字段排序中,越靠前优先级越高,下面中cname1优先排序,当cname1等值的时候,cname2开始排序,直至所有字段都排序完。
- 1 select cname from tname order by cname1 [asc|desc],cname2 [asc|desc]...;
单个字段排序
举个例子,在销售额中通按照交易的订单进行金额额度降序的方式显示:
- 1 mysql> select * from t_order;
- 2 +---------+---------+---------+-------+
- 3 | orderid | account | amount | goods |
- 4 +---------+---------+---------+-------+
- 5 | 8 | brand | 52.2 | 2 |
- 6 | 9 | hen | 1752.02 | 7 |
- 7 | 10 | helyn | 88.5 | 4 |
- 8 | 11 | sol | 1007.9 | 11 |
- 9 | 12 | diny | 12 | 1 |
- 10 | 13 | weng | 52.2 | 5 |
- 11 | 14 | sally | 99.71 | 9 |
- 12 +---------+---------+---------+-------+
- 13 7 rows in set
- 14
- 15 mysql> select * from t_order order by amount desc;
- 16 +---------+---------+---------+-------+
- 17 | orderid | account | amount | goods |
- 18 +---------+---------+---------+-------+
- 19 | 9 | hen | 1752.02 | 7 |
- 20 | 11 | sol | 1007.9 | 11 |
- 21 | 14 | sally | 99.71 | 9 |
- 22 | 10 | helyn | 88.5 | 4 |
- 23 | 8 | brand | 52.2 | 2 |
- 24 | 13 | weng | 52.2 | 5 |
- 25 | 12 | diny | 12 | 1 |
- 26 +---------+---------+---------+-------+
- 27 7 rows in set
多个字段排序
多个字段排序用逗号隔开,优先级从左到右逐次递减,如下图,如果金额一致,则按照购买商品数量从多到少排序:
- 1 mysql> select * from t_order order by amount desc,goods desc;
- 2 +---------+---------+---------+-------+
- 3 | orderid | account | amount | goods |
- 4 +---------+---------+---------+-------+
- 5 | 9 | hen | 1752.02 | 7 |
- 6 | 11 | sol | 1007.9 | 11 |
- 7 | 14 | sally | 99.71 | 9 |
- 8 | 10 | helyn | 88.5 | 4 |
- 9 | 13 | weng | 52.2 | 5 |
- 10 | 8 | brand | 52.2 | 2 |
- 11 | 12 | diny | 12 | 1 |
- 12 +---------+---------+---------+-------+
- 13 7 rows in set
按alias排序
按照别名排序或者做条件查询的目的都是为了简化代码,方便使用,别名可以是英文,也可以是中文:
- 1 mysql> select account as ac,amount as am,goods as gd from t_order order by am,gd desc;
- 2
- 3 +-------+---------+----+
- 4 | ac | am | gd |
- 5 +-------+---------+----+
- 6 | diny | 12 | 1 |
- 7 | weng | 52.2 | 5 |
- 8 | brand | 52.2 | 2 |
- 9 | helyn | 88.5 | 4 |
- 10 | sally | 99.71 | 9 |
- 11 | sol | 1007.9 | 11 |
- 12 | hen | 1752.02 | 7 |
- 13 +-------+---------+----+
- 14 7 rows in set
字段排序中使用函数
下面使用了abs取绝对值函数,所以在 am字段降序排序中,-99.99 排在 99.71之上。
- 1 mysql> select * from t_order;
- 2 +---------+---------+---------+-------+
- 3 | orderid | account | amount | goods |
- 4 +---------+---------+---------+-------+
- 5 | 8 | brand | 52.2 | 2 |
- 6 | 9 | hen | 1752.02 | 7 |
- 7 | 10 | helyn | 88.5 | 4 |
- 8 | 11 | sol | 1007.9 | 11 |
- 9 | 12 | diny | 12 | 1 |
- 10 | 13 | weng | 52.2 | 5 |
- 11 | 14 | sally | 99.71 | 9 |
- 12 | 15 | brand1 | -99.99 | 5 |
- 13 +---------+---------+---------+-------+
- 14 8 rows in set
- 15
- 16 mysql> select account as ac,amount as am,goods as gd from t_order order by abs(am) desc;
- 17
- 18 +--------+---------+----+
- 19 | ac | am | gd |
- 20 +--------+---------+----+
- 21 | hen | 1752.02 | 7 |
- 22 | sol | 1007.9 | 11 |
- 23 | brand1 | -99.99 | 5 |
- 24 | sally | 99.71 | 9 |
- 25 | helyn | 88.5 | 4 |
- 26 | brand | 52.2 | 2 |
- 27 | weng | 52.2 | 5 |
- 28 | diny | 12 | 1 |
- 29 +--------+---------+----+
- 30 8 rows in set
与Where条件结合使用
order 在 where 条件之后,根据where已经过滤好的数据再进行排序。下面是过滤出购买金额>80 且 购买数量>5的数据,并且按照价格降序排序。
- 1 mysql> select * from t_order;
- 2 +---------+---------+---------+-------+
- 3 | orderid | account | amount | goods |
- 4 +---------+---------+---------+-------+
- 5 | 8 | brand | 52.2 | 2 |
- 6 | 9 | hen | 1752.02 | 7 |
- 7 | 10 | helyn | 88.5 | 4 |
- 8 | 11 | sol | 1007.9 | 11 |
- 9 | 12 | diny | 12 | 1 |
- 10 | 13 | weng | 52.2 | 5 |
- 11 | 14 | sally | 99.71 | 9 |
- 12 | 15 | brand1 | -99.99 | 5 |
- 13 +---------+---------+---------+-------+
- 14 8 rows in set
- 15
- 16 mysql> select * from t_order where amount>80 and goods>5 order by amount desc;
- 17 +---------+---------+---------+-------+
- 18 | orderid | account | amount | goods |
- 19 +---------+---------+---------+-------+
- 20 | 9 | hen | 1752.02 | 7 |
- 21 | 11 | sol | 1007.9 | 11 |
- 22 | 14 | sally | 99.71 | 9 |
- 23 +---------+---------+---------+-------+
数据limit
很多时候我们过滤出符合要求的数据之后,还需要得到这些数据中的某一个具体区间,比如对付款超过1000的用户的第1~10,11~20,21~30 名分别赠予不同的礼品,这时候就要使用limit操作了。
limit用来限制select查询返回的数据,常用于数据排行或者分页等情况。
语法格式如下:
- 1 select cname from tname limit [offset,] count;
1、offset表示偏移量,就是指跳过的行数,可以省略不写,默认为0,表示跳过0行,如 limit 8 等同于 limit 0,8。
2、count:跳过偏移量offset之后开始取的数据行数,有count行。
3、limit中offset和count的值不能用表达式。
获取前n条记录
如下图,limit n 和 limit 0,n 是一致的:
- 1 mysql> select * from t_order;
- 2 +---------+---------+---------+-------+
- 3 | orderid | account | amount | goods |
- 4 +---------+---------+---------+-------+
- 5 | 8 | brand | 52.2 | 2 |
- 6 | 9 | hen | 1752.02 | 7 |
- 7 | 10 | helyn | 88.5 | 4 |
- 8 | 11 | sol | 1007.9 | 11 |
- 9 | 12 | diny | 12 | 1 |
- 10 | 13 | weng | 52.2 | 5 |
- 11 | 14 | sally | 99.71 | 9 |
- 12 | 15 | brand1 | -99.99 | 5 |
- 13 +---------+---------+---------+-------+
- 14 8 rows in set
- 15
- 16 mysql> select * from t_order limit 2
- 17 ;
- 18 +---------+---------+---------+-------+
- 19 | orderid | account | amount | goods |
- 20 +---------+---------+---------+-------+
- 21 | 8 | brand | 52.2 | 2 |
- 22 | 9 | hen | 1752.02 | 7 |
- 23 +---------+---------+---------+-------+
- 24 2 rows in set
- 25
- 26 mysql> select * from t_order limit 0,2;
- 27 +---------+---------+---------+-------+
- 28 | orderid | account | amount | goods |
- 29 +---------+---------+---------+-------+
- 30 | 8 | brand | 52.2 | 2 |
- 31 | 9 | hen | 1752.02 | 7 |
- 32 +---------+---------+---------+-------+
- 33 2 rows in set
limit限制单条记录
这边我们获取支付金额中最大和最小的的一条记录。可以先使用 order 条件进行排序,然后limit 第1条记录即可:
- 1 1 mysql> select * from t_order;
- 2 2 +---------+---------+---------+-------+
- 3 3 | orderid | account | amount | goods |
- 4 4 +---------+---------+---------+-------+
- 5 5 | 8 | brand | 52.2 | 2 |
- 6 6 | 9 | hen | 1752.02 | 7 |
- 7 7 | 10 | helyn | 88.5 | 4 |
- 8 8 | 11 | sol | 1007.9 | 11 |
- 9 9 | 12 | diny | 12 | 1 |
- 10 10 | 13 | weng | 52.2 | 5 |
- 11 11 | 14 | sally | 99.71 | 9 |
- 12 12 | 15 | brand1 | -99.99 | 5 |
- 13 13 +---------+---------+---------+-------+
- 14 14 8 rows in set
- 15 15
- 16 16 mysql> select * from t_order where amount>0 order by amount desc limit 1;
- 17 17 +---------+---------+---------+-------+
- 18 18 | orderid | account | amount | goods |
- 19 19 +---------+---------+---------+-------+
- 20 20 | 9 | hen | 1752.02 | 7 |
- 21 21 +---------+---------+---------+-------+
- 22 22 1 row in set
- 23 23
- 24 24 mysql> select * from t_order where amount>0 order by amount asc limit 1;
- 25 25 +---------+---------+--------+-------+
- 26 26 | orderid | account | amount | goods |
- 27 27 +---------+---------+--------+-------+
- 28 28 | 12 | diny | 12 | 1 |
- 29 29 +---------+---------+--------+-------+
- 30 30 1 row in set
- 31
获取(m,n)区间记录
即跳过m条,获取n条,示例如下,我们跳过两条,从第三条开始,连取四条的操作:
- 1 mysql> select * from t_order order by amount;
- 2 +---------+---------+---------+-------+
- 3 | orderid | account | amount | goods |
- 4 +---------+---------+---------+-------+
- 5 | 15 | brand1 | -99.99 | 5 |
- 6 | 12 | diny | 12 | 1 |
- 7 | 8 | brand | 52.2 | 2 |
- 8 | 13 | weng | 52.2 | 5 |
- 9 | 10 | helyn | 88.5 | 4 |
- 10 | 14 | sally | 99.71 | 9 |
- 11 | 11 | sol | 1007.9 | 11 |
- 12 | 9 | hen | 1752.02 | 7 |
- 13 +---------+---------+---------+-------+
- 14 8 rows in set
- 15
- 16 mysql> select * from t_order order by amount limit 2,4;
- 17 +---------+---------+--------+-------+
- 18 | orderid | account | amount | goods |
- 19 +---------+---------+--------+-------+
- 20 | 8 | brand | 52.2 | 2 |
- 21 | 13 | weng | 52.2 | 5 |
- 22 | 10 | helyn | 88.5 | 4 |
- 23 | 14 | sally | 99.71 | 9 |
- 24 +---------+---------+--------+-------+
- 25 4 rows in set
分页的做法与这个类似,我们程序业务上看到的分页一般有 pageIndex,pageSize等参数,我们通常的做法是 limit pageIndex*pageSize,pageSize。
这边假设有31条数据,每页数量pageSize=10,页面索引pageIndex默认0,则第一页就是 limit 0,10,第二页 limit10,10,第三页 limit 20,10,第四页 limit 30,10。
注意点:
1、limit 后面不能使用表达式,只能使用明确的数值,否则会爆出异常,比如 limit 0*10,10,是不对的,这个上面提过了。
2、limit后续的数值只能是正整数和0,也就是说,不能是负数,否则同样会报错。
3、排序字段的值相同情况下,排序后分页会出现混乱重复的情况。
第3点详细说明下:假如根据age排序,但是有多个age都是20岁的同学,这时候我们3条记录一页,就会出现分页混乱数据重复。因为年龄相同的人有多个,这是几个人的排序在每次查询的时候会有不确定性。
举个例子,下面的分页,混乱了:
- 1 mysql> select * from user3;
- 2 +----+------+-------+
- 3 | id | age | name |
- 4 +----+------+-------+
- 5 | 1 | 20 | brand |
- 6 | 2 | 22 | sol |
- 7 | 3 | 20 | helen |
- 8 | 4 | 19.5 | diny |
- 9 | 6 | 19 | a |
- 10 | 7 | 20 | b |
- 11 | 8 | 20 | c |
- 12 | 9 | 20 | d |
- 13 | 10 | 20 | e |
- 14 | 11 | 23 | f |
- 15 +----+------+-------+
- 16 10 rows in set
- 17
- 18 mysql> select * from user3 order by age limit 0,3;
- 19 +----+------+-------+
- 20 | id | age | name |
- 21 +----+------+-------+
- 22 | 6 | 19 | a |
- 23 | 4 | 19.5 | diny |
- 24 | 3 | 20 | helen |
- 25 +----+------+-------+
- 26 3 rows in set
- 27
- 28 mysql> select * from user3 order by age limit 3,3;
- 29
- 30 +----+-----+-------+
- 31 | id | age | name |
- 32 +----+-----+-------+
- 33 | 3 | 20 | helen |
- 34 | 7 | 20 | b |
- 35 | 8 | 20 | c |
- 36 +----+-----+-------+
- 37 3 rows in set
- 38
- 39 mysql> select * from user3 order by age limit 6,3;
- 40
- 41 +----+-----+-------+
- 42 | id | age | name |
- 43 +----+-----+-------+
- 44 | 7 | 20 | b |
- 45 | 3 | 20 | helen |
- 46 | 2 | 22 | sol |
- 47 +----+-----+-------+
- 48 3 rows in set
我们的做法是使用重复值字段做排序的时候再加个唯一依据(一般可以设主键),就不会混乱了。
如下示例,正常了:
- 1 mysql> select * from user3;
- 2 +----+------+-------+
- 3 | id | age | name |
- 4 +----+------+-------+
- 5 | 1 | 20 | brand |
- 6 | 2 | 22 | sol |
- 7 | 3 | 20 | helen |
- 8 | 4 | 19.5 | diny |
- 9 | 6 | 19 | a |
- 10 | 7 | 20 | b |
- 11 | 8 | 20 | c |
- 12 | 9 | 20 | d |
- 13 | 10 | 20 | e |
- 14 | 11 | 23 | f |
- 15 +----+------+-------+
- 16 10 rows in set
- 17
- 18 mysql> select * from user3 order by age,id limit 0,3;
- 19
- 20 +----+------+-------+
- 21 | id | age | name |
- 22 +----+------+-------+
- 23 | 6 | 19 | a |
- 24 | 4 | 19.5 | diny |
- 25 | 1 | 20 | brand |
- 26 +----+------+-------+
- 27 3 rows in set
- 28
- 29 mysql> select * from user3 order by age,id limit 3,3;
- 30
- 31 +----+-----+-------+
- 32 | id | age | name |
- 33 +----+-----+-------+
- 34 | 3 | 20 | helen |
- 35 | 7 | 20 | b |
- 36 | 8 | 20 | c |
- 37 +----+-----+-------+
- 38 3 rows in set
- 39
- 40 mysql> select * from user3 order by age,id limit 6,3;
- 41
- 42 +----+-----+------+
- 43 | id | age | name |
- 44 +----+-----+------+
- 45 | 9 | 20 | d |
- 46 | 10 | 20 | e |
- 47 | 2 | 22 | sol |
- 48 +----+-----+------+
- 49 3 rows in set
上述总结
1、order by cname [asc|desc] 用于对查询结果排序,asc为升序,desc为降序,可以省略,省略情况下默认为asc。
2、limit用来限制查询结果返回的行数,包含2个参数(offset,count),offset:表示跳过多少行,count:表示跳过offset行之后取的行数。limit中offset可以省略,默认值为0;limit中offset 和 count都必须大于等于0;limit中offset和count的值不能用表达式。
3、分页排序时,排序字段不要有重复值,重复值情况下可能会导致分页结果混乱,建议在后面加一个主键排序。
MySQL全面瓦解9:查询的排序、分页相关的更多相关文章
- Oracle 数据库分页查询与排序分页查询
一.分页查询 原始查询语句 SELECT * FROM NASLE_WFSHH 修改为分页查询语句,加上 ROWNUM 列.此处为查询第 1 页,每页 9 条数据 SELECT * FROM ( SE ...
- 10. MySQL基础-02条件查询、排序查询
2. 条件查询 语法 select 查询列表 from 表名 where 筛选条件: 分类 按条件表达式筛选 简单的条件运算符:> < = != <> >= ⇐ 按逻 ...
- Mysql 单表查询-排序-分页-group by初识
Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...
- mysql、sql server、oracle数据库分页查询及分析(操作手册)
1.mysql分页查询 方式1: select * from table order by id limit m, n; 该语句的意思为,查询m+n条记录,去掉前m条,返回后n条记录.无疑该查询能够实 ...
- MySQL之单表查询 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录数:LIMIT 九 使用正则表达式查询
MySQL之单表查询 阅读目录 一 单表查询的语法 二 关键字的执行优先级(重点) 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER B ...
- 【mysql】 mysql 子查询、联合查询、模糊查询、排序、聚合函数、分组----------语法
第二章 mysql 一.模糊查询 like 1. 字段 like '河北省%' %代表任何N个字符 2 字段 like '河北省____' _代表任意1个字符 二.IN 语法:SELECT 字段列1, ...
- MySQL 按照数据库表字段动态排序 查询列表信息
MySQL 按照数据库表字段动态排序 查询列表信息 背景描述 项目中数据列表分页展示的时候,前端使用的Table组件,每列自带对当前页的数据进行升序或者降序的排序. 但是客户期望:随机点击某一列的时候 ...
- es的查询、排序查询、分页查询、布尔查询、查询结果过滤、高亮查询、聚合函数、python操作es
今日内容概要 es的查询 Elasticsearch之排序查询 Elasticsearch之分页查询 Elasticsearch之布尔查询 Elasticsearch之查询结果过滤 Elasticse ...
- 在ASP.NET MVC中使用Boostrap实现产品的展示、查询、排序、分页
在产品展示中,通常涉及产品的展示方式.查询.排序.分页,本篇就在ASP.NET MVC下,使用Boostrap来实现. 源码放在了GitHub: https://github.com/darrenji ...
随机推荐
- 1.入门篇十分钟了解Spring Cloud
文章目录 Spring Cloud入门系列汇总 为什么需要学习Spring Cloud 什么是Spring Cloud 设计目标与优缺点 设计目标 优缺点 Spring Cloud发展前景 整体架构 ...
- 极简 Node.js 入门 - 5.1 创建 HTTP 服务器
极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...
- http_parser
最近读了 http_parser 的源码,记录下. 有意思的地方: 1) 协议解析可以不完全解析完,但是当前 parser 会记录解析状态,这样可以继续解析 2) 协议解析首要还是要了解协议 ...
- 关于android和Linux的一些问题
1.Android为什么选择java? 由于java虚拟机,实现软件层的编程与硬件无关性(无需进行特定编译或平台配置). 2.Android和Linux内核区别? Android上的应用软件运行在da ...
- Java 合并Word文档
合并文档可以是将两个包含一定逻辑关系的文档合并成一个完整的文档,也可以是出于方便文档存储.管理的目的合并多个文档为一个文档.下面,就将以上文档操作需求,通过Java程序来实现Word文档合并.合并文档 ...
- sessionFactory' defined in class path /mappingDirectoryLocations配置问题
问题:配置好aplicationContext.xml,启动tomcat 出现如下问题 sessionFactory无法正常建立 Context initialization failed org.s ...
- CSS语法规范与代码风格
CSS语法规范与代码风格 1. 语法规范 CSS规则又两个主要的部分构成:选择器+一条或多条声明. 选择器:用于指定CSS样式的HTML标签,花括号内的是设置的具体样式 属性与属性值以键值对的形式出现 ...
- .Net Core中使用Grpc
一.Grpc概述 gRPC 基于如下思想:定义一个服务, 指定其可以被远程调用的方法及其参数和返回类型.gRPC 默认使用protocol buffers作为接口定义语言,来描述服务接口和有效载荷消息 ...
- Git软件操作过程
一.下载 Git 二.下载Git小乌龟-TortoiseGit 三.汉化-去官网下载,官网地址 https://tortoisegit.org/download/
- Java Map转成xml标签字符串
一个简单的java实现,供参考: package com.trilogy.session.data; import java.lang.reflect.Field; import java.util. ...