时间:2021-07-01 10:21:17 帮助过:133人阅读
ORDER BY 语句用于根据指定的列对结果集进行排序。
ORDER BY 语句默认按照升序对记录进行排序。
如果您希望按照降序对记录进行排序,可以使用 DESC 关键字。
Orders 表:
Company | OrderNumber |
---|---|
IBM | 3532 |
W3School | 2356 |
Apple | 4698 |
W3School | 6953 |
以字母顺序显示公司名称:
SELECT Company, OrderNumber FROM Orders ORDER BY Company
Company | OrderNumber |
---|---|
Apple | 4698 |
IBM | 3532 |
W3School | 6953 |
W3School | 2356 |
以字母顺序显示公司名称(Company),并以数字顺序显示顺序号(OrderNumber):
SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber
结果:
Company | OrderNumber |
---|---|
Apple | 4698 |
IBM | 3532 |
W3School | 2356 |
W3School | 6953 |
以逆字母顺序显示公司名称:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC
Company | OrderNumber |
---|---|
W3School | 6953 |
W3School | 2356 |
IBM | 3532 |
Apple | 4698 |
以逆字母顺序显示公司名称,并以数字顺序显示顺序号:
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC
Company | OrderNumber |
---|---|
W3School | 2356 |
W3School | 6953 |
IBM | 3532 |
Apple | 4698 |