扫码一下
查看教程更方便
sqlite 的 order by 语句是用来基于一个或多个列按升序或降序顺序排列数据。
order by 子句的基本语法如下:
select column-list
from table_name
[where condition]
[order by column1, column2, .. columnn] [asc | desc];
可以在 order by 子句中使用多个列。确保您使用的排序列在列清单中。
假设 company 表有以下记录:
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 升序排序:
sqlite> select * from company order by salary asc;
结果如下:
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 升序排序:
sqlite> select * from company order by name, salary asc;
结果如下:
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 降序排序:
sqlite> select * from company order by name desc;
结果如下:
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