默认为升序,如果希望按降序对记录进行排序,可使用 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 |