Home  >  Article  >  Database  >  SQL ORDER BY 语句用法

SQL ORDER BY 语句用法

WBOY
WBOYOriginal
2016-06-07 17:47:012575browse

ORDER BY 语句用于对结果集进行排序。

ORDER BY 语句

ORDER BY 语句用于根据指定的列对结果集进行排序。

ORDER BY 语句默认按照升序对记录进行排序。

如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。

原始的表 (用在例子中的):

Orders 表:

Company OrderNumber
IBM 3532
W3School 2356
Apple 4698
W3School 6953

实例 1

以字母顺序显示公司名称:

SELECT Company, OrderNumber FROM Orders <code>ORDER BY Company</code>

结果:

Company OrderNumber
Apple 4698
IBM 3532
W3School 6953
W3School 2356

实例 2

以字母顺序显示公司名称(Company),并以数字顺序显示顺序号(OrderNumber):

SELECT Company, OrderNumber FROM Orders <code>ORDER BY Company, OrderNumber</code>

结果:

Company OrderNumber
Apple 4698
IBM 3532
W3School 2356
W3School 6953

实例 3

以逆字母顺序显示公司名称:

SELECT Company, OrderNumber FROM Orders <code>ORDER BY Company DESC</code>

结果:

Company OrderNumber
W3School 6953
W3School 2356
IBM 3532
Apple 4698

实例 4

以逆字母顺序显示公司名称,并以数字顺序显示顺序号:

SELECT Company, OrderNumber FROM Orders <code>ORDER BY Company DESC, OrderNumber ASC</code>

结果:

Company OrderNumber
W3School 2356
W3School 6953
IBM 3532
Apple 4698
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn