【SQL语法】系列06:利用ORDER BY排序

默认为升序,如果希望按降序对记录进行排序,可使用 DESC 关键字。

Orders 表:

Company

OrderNumber

IBM

3532

W3School

2356

Apple

4698

W3School

6953

1

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

SELECT Company, OrderNumber FROM Orders ORDER BY Company

结果:

Company

OrderNumber

Apple

4698

IBM

3532

W3School

6953

W3School

2356

2

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

SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber

结果:

Company

OrderNumber

Apple

4698

IBM

3532

W3School

2356

W3School

6953

3

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

SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC

结果:

Company

OrderNumber

W3School

6953

W3School

2356

IBM

3532

Apple

4698

4

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

SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC

结果:

Company

OrderNumber

W3School

2356

W3School

6953

IBM

3532

Apple

4698

你可能感兴趣的:(order by)