第 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 的组合,能够找到一个列中最高或最低值