MySQL 保姆级教程(三):排序检索数据

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
云数据库 RDS PostgreSQL,高可用系列 2核4GB
简介: MySQL 保姆级教程(三):排序检索数据

第 5 章 排序检索数据

5.1 排序数据

输入: SELECT help_category.name FROM help_category ORDER BY help_category.name;
输出: 
+---------------------------------------+
| name                                  |
+---------------------------------------+
| Account Management                    |
| Administration                        |
| Aggregate Functions and Modifiers     |
| Bit Functions                         |
| Cast Functions and Operators          |
| Comparison Operators                  |
| Components                            |
| Compound Statements                   |
| Contents                              |
| Data Definition                       |
| Data Manipulation                     |
| Data Types                            |
| Date and Time Functions               |
| Encryption Functions                  |
| Enterprise Encryption Functions       |
| Flow Control Functions                |
| Functions                             |
| Geographic Features                   |
| Geometry Constructors                 |
| Geometry Property Functions           |
| Geometry Relation Functions           |
| GeometryCollection Property Functions |
| GROUP BY Functions and Modifiers      |
| GTID                                  |
| Help Metadata                         |
| Information Functions                 |
| Internal Functions                    |
| Language Structure                    |
| LineString Property Functions         |
| Loadable Functions                    |
| Locking Functions                     |
| Logical Operators                     |
| MBR                                   |
| MBR Functions                         |
| Miscellaneous Functions               |
| Numeric Functions                     |
| Performance Schema Functions          |
| Plugins                               |
| Point Property Functions              |
| Polygon Property Functions            |
| Prepared Statements                   |
| Replication Statements                |
| Spatial Functions                     |
| Storage Engines                       |
| String Functions                      |
| Table Maintenance                     |
| Transactions                          |
| Utility                               |
| Window Functions                      |
| WKB Functions                         |
| WKT                                   |
| WKT Functions                         |
| XML                                   |
+---------------------------------------+
分析: order by 子句用于对查询结果进行排序,默认升序

5.2 按多个列排序

输入: SELECT help_category_id,help_category.name,parent_category_id FROM help_category ORDER BY help_category.name;
输出: 
+------------------+---------------------------------------+--------------------+
| help_category_id | name                                  | parent_category_id |
+------------------+---------------------------------------+--------------------+
|               46 | Account Management                    |                  0 |
|                3 | Administration                        |                  0 |
|               34 | Aggregate Functions and Modifiers     |                  4 |
|               18 | Bit Functions                         |                  4 |
|               16 | Cast Functions and Operators          |                  4 |
|               10 | Comparison Operators                  |                  4 |
|               49 | Components                            |                  0 |
|               45 | Compound Statements                   |                  0 |
|                0 | Contents                              |                  0 |
|               40 | Data Definition                       |                  0 |
|               41 | Data Manipulation                     |                  0 |
|                2 | Data Types                            |                  0 |
|               14 | Date and Time Functions               |                  4 |
|               19 | Encryption Functions                  |                  4 |
|                5 | Enterprise Encryption Functions       |                  4 |
|               12 | Flow Control Functions                |                  4 |
|                4 | Functions                             |                  0 |
|                7 | Geographic Features                   |                  0 |
|               25 | Geometry Constructors                 |                 22 |
|               26 | Geometry Property Functions           |                 22 |
|               31 | Geometry Relation Functions           |                 22 |
|               30 | GeometryCollection Property Functions |                 22 |
|               35 | GROUP BY Functions and Modifiers      |                  4 |
|               33 | GTID                                  |                  4 |
|                1 | Help Metadata                         |                  0 |
|               21 | Information Functions                 |                  4 |
|               38 | Internal Functions                    |                  4 |
|                6 | Language Structure                    |                  0 |
|               28 | LineString Property Functions         |                 22 |
|               48 | Loadable Functions                    |                  0 |
|               20 | Locking Functions                     |                  4 |
|               11 | Logical Operators                     |                  4 |
|                8 | MBR                                   |                  7 |
|               32 | MBR Functions                         |                 22 |
|               39 | Miscellaneous Functions               |                  4 |
|               13 | Numeric Functions                     |                  4 |
|               37 | Performance Schema Functions          |                  4 |
|               50 | Plugins                               |                  0 |
|               27 | Point Property Functions              |                 22 |
|               29 | Polygon Property Functions            |                 22 |
|               44 | Prepared Statements                   |                  0 |
|               43 | Replication Statements                |                  0 |
|               22 | Spatial Functions                     |                  4 |
|               52 | Storage Engines                       |                  0 |
|               15 | String Functions                      |                  4 |
|               47 | Table Maintenance                     |                  0 |
|               42 | Transactions                          |                  0 |
|               51 | Utility                               |                  0 |
|               36 | Window Functions                      |                  4 |
|               24 | WKB Functions                         |                 22 |
|                9 | WKT                                   |                  7 |
|               23 | WKT Functions                         |                 22 |
|               17 | XML                                   |                  4 |
+------------------+---------------------------------------+--------------------+

5.3 指定排序方向

输入: SELECT help_category.name FROM help_category ORDER BY help_category.name DESC;
输出: 
+---------------------------------------+
| name                                  |
+---------------------------------------+
| XML                                   |
| WKT Functions                         |
| WKT                                   |
| WKB Functions                         |
| Window Functions                      |
| Utility                               |
| Transactions                          |
| Table Maintenance                     |
| String Functions                      |
| Storage Engines                       |
| Spatial Functions                     |
| Replication Statements                |
| Prepared Statements                   |
| Polygon Property Functions            |
| Point Property Functions              |
| Plugins                               |
| Performance Schema Functions          |
| Numeric Functions                     |
| Miscellaneous Functions               |
| MBR Functions                         |
| MBR                                   |
| Logical Operators                     |
| Locking Functions                     |
| Loadable Functions                    |
| LineString Property Functions         |
| Language Structure                    |
| Internal Functions                    |
| Information Functions                 |
| Help Metadata                         |
| GTID                                  |
| GROUP BY Functions and Modifiers      |
| GeometryCollection Property Functions |
| Geometry Relation Functions           |
| Geometry Property Functions           |
| Geometry Constructors                 |
| Geographic Features                   |
| Functions                             |
| Flow Control Functions                |
| Enterprise Encryption Functions       |
| Encryption Functions                  |
| Date and Time Functions               |
| Data Types                            |
| Data Manipulation                     |
| Data Definition                       |
| Contents                              |
| Compound Statements                   |
| Components                            |
| Comparison Operators                  |
| Cast Functions and Operators          |
| Bit Functions                         |
| Aggregate Functions and Modifiers     |
| Administration                        |
| Account Management                    |
+---------------------------------------+
分析: 如果你想按照降序顺序排序,可以添加 desc 关键字在列名后面
例如: 使用 order by 和 LIMIT 的组合,能够找到一个列中最高或最低值
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
缓存 NoSQL 关系型数据库
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
|
4天前
|
SQL 人工智能 关系型数据库
如何实现MySQL百万级数据的查询?
本文探讨了在MySQL中对百万级数据进行排序分页查询的优化策略。面对五百万条数据,传统的浅分页和深分页查询效率较低,尤其深分页因偏移量大导致性能显著下降。通过为排序字段添加索引、使用联合索引、手动回表等方法,有效提升了查询速度。最终建议根据业务需求选择合适方案:浅分页可加单列索引,深分页推荐联合索引或子查询优化,同时结合前端传递最后一条数据ID的方式实现高效翻页。
|
2月前
|
存储 关系型数据库 MySQL
【免费动手教程上线】阿里云RDS MySQL推出大容量高性能存储:高性能本地盘(最高16TB存储空间)、高性能云盘(最高64TB存储空间)
阿里云RDS MySQL提供高性能本地盘与高性能云盘等存储方案,满足用户大容量、低延迟需求。高性能本地盘单盘最大16TB,IO延时微秒级;高性能云盘兼容ESSD特性,支持IO性能突发、BPE及16K原子写等能力。此外,阿里云还提供免费动手体验教程,帮助用户直观感受云数据库 RDS 存储性能表现。
|
3月前
|
关系型数据库 MySQL Linux
在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾
以上就是在Linux环境下备份Docker中的MySQL数据并传输到其他服务器以实现数据级别的容灾的步骤。这个过程就像是一场接力赛,数据从MySQL数据库中接力棒一样传递到备份文件,再从备份文件传递到其他服务器,最后再传递回MySQL数据库。这样,即使在灾难发生时,我们也可以快速恢复数据,保证业务的正常运行。
198 28
|
2月前
|
存储 SQL 缓存
mysql数据引擎有哪些
MySQL 提供了多种存储引擎,每种引擎都有其独特的特点和适用场景。以下是一些常见的 MySQL 存储引擎及其特点:
91 0
|
4月前
|
SQL 关系型数据库 MySQL
【YashanDB知识库】字符集latin1的MySQL中文数据如何迁移到YashanDB
本文探讨了在使用YMP 23.2.1.3迁移MySQL Server字符集为latin1的中文数据至YashanDB时出现乱码的问题。问题根源在于MySQL latin1字符集存放的是实际utf8编码的数据,而YMP尚未支持此类场景。文章提供了两种解决方法:一是通过DBeaver直接迁移表数据;二是将MySQL表数据转换为Insert语句后手动插入YashanDB。同时指出,这两种方法适合单张表迁移,多表迁移可能存在兼容性问题,建议对问题表单独处理。
【YashanDB知识库】字符集latin1的MySQL中文数据如何迁移到YashanDB
|
1月前
|
人工智能 运维 关系型数据库
数据库运维:mysql 数据库迁移方法-mysqldump
本文介绍了MySQL数据库迁移的方法与技巧,重点探讨了数据量大小对迁移方式的影响。对于10GB以下的小型数据库,推荐使用mysqldump进行逻辑导出和source导入;10GB以上可考虑mydumper与myloader工具;100GB以上则建议物理迁移。文中还提供了统计数据库及表空间大小的SQL语句,并讲解了如何使用mysqldump导出存储过程、函数和数据结构。通过结合实际应用场景选择合适的工具与方法,可实现高效的数据迁移。
242 1
|
2月前
|
负载均衡 算法 关系型数据库
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
本文聚焦 MySQL 集群架构中的负载均衡算法,阐述其重要性。详细介绍轮询、加权轮询、最少连接、加权最少连接、随机、源地址哈希等常用算法,分析各自优缺点及适用场景。并提供 Java 语言代码实现示例,助力直观理解。文章结构清晰,语言通俗易懂,对理解和应用负载均衡算法具有实用价值和参考价值。
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
|
4天前
|
SQL 存储 关系型数据库
MySQL功能模块探秘:数据库世界的奇妙之旅
]带你轻松愉快地探索MySQL 8.4.5的核心功能模块,从SQL引擎到存储引擎,从复制机制到插件系统,让你在欢声笑语中掌握数据库的精髓!
50 26
|
25天前
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
111 62

推荐镜像

更多