SQL: ORDER BY Clause
The ORDER BY clause allows you to sort the records in your result set. The ORDER BY clause can only be used in SELECT statements.
译:
ORDER BY
允许你在结果集中对记录进行排序。
ORDER BY
只能够用于
SELECT
语句。
The syntax for the ORDER BY clause is:
译:
ORDER BY
的语法如下:
SELECT columns
FROM tables
WHERE predicates
ORDER BY column ASC/DESC;
The ORDER BY clause sorts the result set based on the columns specified. If the ASC or DESC value is omitted, the system assumed ascending order.
译:
ORDER BY
是根据指定的列进行排序。如果省略
ASC
或者
DESC
,系统默认为升序。
ASC
indicates ascending order. (default)
DESC indicates descending order.
译:
ASC
表明升序(默认)
DESC
表明降序。
Example #1
SELECT supplier_city
FROM supplier
WHERE supplier_name = 'IBM'
ORDER BY supplier_city;
This would return all records sorted by the supplier_city field in ascending order.
译:结果将会以
supplier_city
排序的升序结果返回所有记录。
Example #2
SELECT supplier_city
FROM supplier
WHERE supplier_name = 'IBM'
ORDER BY supplier_city DESC;
This would return all records sorted by the supplier_city field in descending order.
译:以
supplier_city
的降序结果返回所有记录。
Example #3
You can also sort by relative position in the result set, where the first field in the result set is 1. The next field is 2, and so on.
译:你可以采用字段的相对位置作为排序,第一个字段为
1
,下一个为
2
等等。
SELECT supplier_city
FROM supplier
WHERE supplier_name = 'IBM'
ORDER BY 1 DESC;
This would return all records sorted by the supplier_city field in descending order, since the supplier_city field is in position #1 in the result set.
译:这样会以
supplier_city
的降序返回所有结果,因为
supplier_city
在结果集中的位置是
#1
。
Example #4
SELECT supplier_city, supplier_state
FROM supplier
WHERE supplier_name = 'IBM'
ORDER BY supplier_city DESC, supplier_state ASC;
This would return all records sorted by the supplier_city field in descending order, with a secondary sort by supplier_state in ascending order.
译:返回以
supplier_city
为降序、
supplier_state
为二级升序的结果。