Home > Database > Mysql Tutorial > SQL ORDER BY 语句用法

SQL ORDER BY 语句用法

WBOY
Release: 2016-06-07 17:47:01
Original
2618 people have browsed it

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>
Copy after login

结果:

Company OrderNumber
Apple 4698
IBM 3532
W3School 6953
W3School 2356

实例 2

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

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

结果:

Company OrderNumber
Apple 4698
IBM 3532
W3School 2356
W3School 6953

实例 3

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

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

结果:

Company OrderNumber
W3School 6953
W3School 2356
IBM 3532
Apple 4698

实例 4

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

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

结果:

Company OrderNumber
W3School 2356
W3School 6953
IBM 3532
Apple 4698
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template