MySQL ORDER BY 关键字
MySQL ORDER BY 关键字
ORDER BY 关键字用于对结果集进行升序或降序排序。
ORDER BY 关键字默认按升序(ASC)对记录进行排序。
要按降序对记录进行排序,请使用 DESC 关键字。
实例
按价格从低到高对产品进行排序:
SELECT * FROM Products ORDER BY Price;
ORDER BY 语法
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|DESC;
演示数据库
以下是 Northwind 演示数据库中 Products 表的部分片段:
| ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
|---|---|---|---|---|---|
| 1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
| 2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
| 3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
| 4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22 |
| 5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35 |
DESC
要按降序对记录进行排序,请使用 DESC 关键字。
实例
按价格从高到低对产品进行排序:
SELECT * FROM Products ORDER BY Price DESC;
按字母顺序排序
对于字符串值,ORDER BY 关键字将按字母顺序排序:
实例
按产品名称的字母顺序对产品进行排序:
SELECT * FROM Products ORDER BY ProductName;
按字母降序排序
要按字母逆序对表进行排序,请使用 DESC 关键字:
实例
按产品名称的逆序对产品进行排序:
SELECT * FROM Products ORDER BY ProductName DESC;
按多列排序
以下 SQL 语句从 "Customers" 表中选择所有客户,并按 "Country" 和 "CustomerName" 列进行排序。
这意味着它按国家排序,但如果某些行具有相同的国家,则按 CustomerName 对它们进行排序:
实例
SELECT * FROM Customers ORDER BY Country, CustomerName;
同时使用 ASC 和 DESC
以下 SQL 语句从 "Customers" 表中选择所有客户,并按 "Country" 列升序和 "CustomerName" 列降序进行排序:
实例
SELECT * FROM Customers ORDER BY Country ASC, CustomerName DESC;