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

简介: 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 的组合,能够找到一个列中最高或最低值
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
缓存 NoSQL 关系型数据库
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
美团面试:MySQL有1000w数据,redis只存20w的数据,如何做 缓存 设计?
|
12月前
|
SQL 人工智能 关系型数据库
如何实现MySQL百万级数据的查询?
本文探讨了在MySQL中对百万级数据进行排序分页查询的优化策略。面对五百万条数据,传统的浅分页和深分页查询效率较低,尤其深分页因偏移量大导致性能显著下降。通过为排序字段添加索引、使用联合索引、手动回表等方法,有效提升了查询速度。最终建议根据业务需求选择合适方案:浅分页可加单列索引,深分页推荐联合索引或子查询优化,同时结合前端传递最后一条数据ID的方式实现高效翻页。
582 0
|
10月前
|
安全 关系型数据库 MySQL
CentOS 7 yum 安装 MySQL教程
在CentOS 7上安装MySQL 8,其实流程很清晰。首先通过官方Yum仓库来安装服务,然后启动并设为开机自启。最重要的环节是首次安全设置:需要先从日志里找到临时密码来登录,再修改成你自己的密码,并为远程连接创建用户和授权。最后,也别忘了在服务器防火墙上放行3306端口,这样远程才能连上。
2445 16
|
11月前
|
存储 关系型数据库 MySQL
在CentOS 8.x上安装Percona Xtrabackup工具备份MySQL数据步骤。
以上就是在CentOS8.x上通过Perconaxtabbackup工具对Mysql进行高效率、高可靠性、无锁定影响地实现在线快速全量及增加式数据库资料保存与恢复流程。通过以上流程可以有效地将Mysql相关资料按需求完成定期或不定期地保存与灾难恢复需求。
777 10
|
12月前
|
SQL 存储 缓存
MySQL 如何高效可靠处理持久化数据
本文详细解析了 MySQL 的 SQL 执行流程、crash-safe 机制及性能优化策略。内容涵盖连接器、分析器、优化器、执行器与存储引擎的工作原理,深入探讨 redolog 与 binlog 的两阶段提交机制,并分析日志策略、组提交、脏页刷盘等关键性能优化手段,帮助提升数据库稳定性与执行效率。
300 0
|
10月前
|
缓存 关系型数据库 BI
使用MYSQL Report分析数据库性能(下)
使用MYSQL Report分析数据库性能
589 158
|
10月前
|
关系型数据库 MySQL 数据库
自建数据库如何迁移至RDS MySQL实例
数据库迁移是一项复杂且耗时的工程,需考虑数据安全、完整性及业务中断影响。使用阿里云数据传输服务DTS,可快速、平滑完成迁移任务,将应用停机时间降至分钟级。您还可通过全量备份自建数据库并恢复至RDS MySQL实例,实现间接迁移上云。
|
10月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1572 152
|
10月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1095 156

推荐镜像

更多