SQLite Order By
sqlite order by
sqlite 的 order by 子句是用来基于一个或多个列按升序或降序顺序排列数据。
1. 语法
order by 子句的基本语法如下:
select column-list from table_name [where condition] [order by column1, column2, .. columnn] [asc | desc];
您可以在 order by 子句中使用多个列。确保您使用的排序列在列清单中。
2. 范例
假设 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