bczd

SQL 按关键字排序

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

分类:

技术点:

相关文章:

  • 2021-11-26
  • 2021-11-17
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-12-22
  • 2022-02-01
猜你喜欢
  • 2022-02-08
  • 2021-11-27
  • 2022-02-15
  • 2021-12-15
  • 2021-12-26
  • 2021-11-08
  • 2022-02-09
相关资源
相似解决方案