SQL ORDER BY Keyword(按关键字排序)
ORDER BY 关键字用于对结果集进行排序。
SQL ORDER BY 关键字
ORDER BY 关键字用于按升序或降序对结果集进行排序。
ORDER BY 关键字默认情况下按升序排序记录。
如果需要按降序对记录进行排序,可以使用DESC关键字。
SQL ORDER BY 语法
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
演示数据库
在本教程中,我们将使用著名的Northwind示例数据库。
以下是 "Customers" 表中的数据:
CustomerID |
CustomerName |
ContactName |
Address |
City |
PostalCode |
Country |
---|---|---|---|---|---|---|
1 |
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
2 |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitución 2222 |
México D.F. |
05021 |
Mexico |
3 |
Antonio Moreno Taquería |
Antonio Moreno |
Mataderos 2312 |
México D.F. |
05023 |
Mexico |
4 |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
5 |
Berglunds snabbk?p |
Christina Berglund |
Berguvsv?gen 8 |
Lule? |
S-958 22 |
Sweden |
ORDER BY 实例
下面的 SQL 语句从 "Customers" 表中选取所有客户,并按照 "Country" 列排序:
实例
SELECT * FROM Customers
ORDER BY Country;
ORDER BY DESC 实例
下面的 SQL 语句从 "Customers" 表中选取所有客户,并按照 "Country" 列降序排序:
实例
SELECT * FROM Customers
ORDER BY Country DESC;
ORDER BY 多列 实例
下面的 SQL 语句从 "Customers" 表中选取所有客户,并按照 "Country" 和 "CustomerName" 列排序:
实例
SELECT * FROM Customers
ORDER BY Country, CustomerName;
ORDER BY 多列 实例2
以下SQL语句从"Customers" 表中选择所有客户,按 "Country" 升序排列,并按 "CustomerName" 列降序排列:
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
本文转自:http://codingdict.com/article/6953
原文地址:https://www.cnblogs.com/bczd/p/11988187.html
时间: 2024-10-10 10:59:52