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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 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 的组合,能够找到一个列中最高或最低值
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2天前
|
关系型数据库 MySQL 数据库
MySQL 复制A的表结构和数据到表B
在MySQL中复制表A至表B可通过不同方法实现。一种是先用`CREATE TABLE B LIKE A;`复制结构,再用`INSERT INTO B SELECT * FROM A;`填充数据。另一种更简便的方法是直接使用`CREATE TABLE B AS SELECT * FROM A;`一次性完成结构和数据的复制。还有一种高级方法是通过`SHOW CREATE TABLE A;`获取表A的创建语句,手动调整后创建表B,如有需要再用`INSERT INTO ... SELECT`复制数据。注意权限问题、跨数据库复制时需指定数据库名,以及大表复制时可能影响性能。
|
5天前
|
消息中间件 数据采集 关系型数据库
大数据-业务数据采集-FlinkCDC 读取 MySQL 数据存入 Kafka
大数据-业务数据采集-FlinkCDC 读取 MySQL 数据存入 Kafka
20 1
|
5天前
|
数据采集 关系型数据库 MySQL
大数据-业务数据采集-FlinkCDC The MySQL server is not configured to use a ROW binlog_format
大数据-业务数据采集-FlinkCDC The MySQL server is not configured to use a ROW binlog_format
15 1
|
1天前
|
canal 关系型数据库 MySQL
"揭秘阿里数据同步黑科技Canal:从原理到实战,手把手教你玩转MySQL数据秒级同步,让你的数据处理能力瞬间飙升,成为技术界的新晋网红!"
【8月更文挑战第18天】Canal是一款由阿里巴巴开源的高性能数据同步系统,它通过解析MySQL的增量日志(Binlog),提供低延迟、可靠的数据订阅和消费功能。Canal模拟MySQL Slave与Master间的交互协议来接收并解析Binary Log,支持数据的增量同步。配置简单直观,包括Server和Instance两层配置。在实战中,Canal可用于数据库镜像、实时备份等多种场景,通过集成Canal Client可实现数据的消费和处理,如更新缓存或写入消息队列。
8 0
|
5天前
|
存储 关系型数据库 MySQL
MySQL——数据库备份上传到阿里云OSS存储
MySQL——数据库备份上传到阿里云OSS存储
18 0
|
8天前
|
SQL 存储 关系型数据库
"MySQL增列必锁表?揭秘InnoDB在线DDL,让你的数据库操作飞一般,性能无忧!"
【8月更文挑战第11天】在数据库领域,MySQL凭借其稳定高效的表现深受开发者喜爱。对于是否会在给数据表添加列时锁表的问题,MySQL的行为受版本、存储引擎等因素影响。从5.6版起,InnoDB支持在线DDL,可在改动表结构时保持表的可访问性,避免长时间锁表。而MyISAM等则需锁表完成操作。例如,在使用InnoDB的表上运行`ALTER TABLE users ADD COLUMN email VARCHAR(255);`时,通常不会完全锁表。虽然在线DDL提高了灵活性,但复杂操作或大表变更仍可能暂时影响性能。因此,进行结构变更前应评估其影响并择机执行。
29 6
|
5天前
|
缓存 NoSQL Redis
一天五道Java面试题----第九天(简述MySQL中索引类型对数据库的性能的影响--------->缓存雪崩、缓存穿透、缓存击穿)
这篇文章是关于Java面试中可能会遇到的五个问题,包括MySQL索引类型及其对数据库性能的影响、Redis的RDB和AOF持久化机制、Redis的过期键删除策略、Redis的单线程模型为何高效,以及缓存雪崩、缓存穿透和缓存击穿的概念及其解决方案。
|
7天前
|
Oracle 关系型数据库 MySQL
Mysql和Oracle数据库死锁查看以及解决
【8月更文挑战第11天】本文介绍了解决MySQL与Oracle数据库死锁的方法。MySQL可通过`SHOW ENGINE INNODB STATUS`查看死锁详情,并自动回滚一个事务解除死锁;也可手动KILL事务。Oracle则通过查询V$LOCK与V$SESSION视图定位死锁,并用`ALTER SYSTEM KILL SESSION`命令终止相关会话。预防措施包括遵循ACID原则、优化索引及拆分大型事务。
|
8天前
|
固态存储 关系型数据库 MySQL
"惊!20亿数据秒速入MySQL,揭秘数据库极速插入的黑科技,你不可不知的绝密技巧!"
【8月更文挑战第11天】面对20亿级数据量,高效插入MySQL成为挑战。本文探讨优化策略:合理设计数据库减少不必要的字段和索引;使用批量插入减少网络往返;优化硬件如SSD和内存及调整MySQL配置;并行处理加速插入;附Python示例代码实现分批导入。这些方法将有效提升大规模数据处理能力。
23 2
|
10天前
|
存储 关系型数据库 MySQL
"Linux环境下MySQL数据库名及表名大小写敏感性设置详解:从配置到影响,确保数据库操作的准确与高效"
【8月更文挑战第9天】在Linux环境中,MySQL数据库名及表名的大小写敏感性是一项重要配置。默认情况下,MySQL在Linux上区分大小写,但这可通过配置文件 `/etc/my.cnf` 中的 `lower_case_table_names` 参数调整。该参数设为0时,名称存储时保持原样,查询时不区分大小写;设为1则全部转换为小写。通过编辑配置文件并重启MySQL服务,可根据需求灵活控制名称的大小写敏感性,确保数据一致性和应用兼容性。
27 3