Sqlite 简明教程
SQLite - ORDER BY Clause
SQLite ORDER BY 子句用于基于一列或多列按升序或降序对数据进行排序。
SQLite ORDER BY clause is used to sort the data in an ascending or descending order, based on one or more columns.
Syntax
以下是 ORDER BY 子句的基本语法。
Following is the basic syntax of ORDER BY clause.
SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];
您可以在 ORDER BY 子句中使用多列。确保您用于排序的列在列列表中可用。
You can use more than one column in the ORDER BY clause. Make sure whatever column you are using to sort, that column should be available in the column-list.
Example
考虑具有以下记录的 COMPANY 表。
Consider COMPANY table with the following records.
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
以下是一个示例,它将按 SALARY 降序对结果进行排序。
Following is an example, which will sort the result in descending order by SALARY.
sqlite> SELECT * FROM COMPANY ORDER BY SALARY ASC;
这将产生以下结果。
This will produce the following result.
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
7 James 24 Houston 10000.0
2 Allen 25 Texas 15000.0
1 Paul 32 California 20000.0
3 Teddy 23 Norway 20000.0
6 Kim 22 South-Hall 45000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
以下是一个示例,它将按 NAME 和 SALARY 降序对结果进行排序。
Following is an example, which will sort the result in descending order by NAME and SALARY.
sqlite> SELECT * FROM COMPANY ORDER BY NAME, SALARY ASC;
这将产生以下结果。
This will produce the following result.
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
2 Allen 25 Texas 15000.0
5 David 27 Texas 85000.0
7 James 24 Houston 10000.0
6 Kim 22 South-Hall 45000.0
4 Mark 25 Rich-Mond 65000.0
1 Paul 32 California 20000.0
3 Teddy 23 Norway 20000.0
以下是一个示例,它将按 NAME 降序对结果进行排序。
Following is an example, which will sort the result in descending order by NAME.
sqlite> SELECT * FROM COMPANY ORDER BY NAME DESC;
这将产生以下结果。
This will produce the following result.
ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
3 Teddy 23 Norway 20000.0
1 Paul 32 California 20000.0
4 Mark 25 Rich-Mond 65000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0
5 David 27 Texas 85000.0
2 Allen 25 Texas 15000.0