Impala 简明教程
Impala - Order By Clause
Impala ORDER BY 从句用于根据一个或多个列以升序或降序对数据进行排序。默认情况下,一些数据库会按升序对查询结果进行排序。
Syntax
以下是 ORDER BY 从句的语法。
select * from table_name ORDER BY col_name [ASC|DESC] [NULLS FIRST|NULLS LAST]
你可以分别使用关键字 ASC 或 DESC 以升序或降序的方式排列表中的数据。
同样,如果我们使用 NULLS FIRST,表中的所有 null 值都会排列在顶部行;如果我们使用 NULLS LAST,包含 null 值的行将被排列在最后。
Example
假设数据库 my_db 中有一个名为 customers 的表,其内容如下 −
[quickstart.cloudera:21000] > select * from customers;
Query: select * from customers
+----+----------+-----+-----------+--------+
| id | name | age | address | salary |
+----+----------+-----+-----------+--------+
| 3 | kaushik | 23 | Kota | 30000 |
| 1 | Ramesh | 32 | Ahmedabad | 20000 |
| 2 | Khilan | 25 | Delhi | 15000 |
| 6 | Komal | 22 | MP | 32000 |
| 4 | Chaitali | 25 | Mumbai | 35000 |
| 5 | Hardik | 27 | Bhopal | 40000 |
+----+----------+-----+-----------+--------+
Fetched 6 row(s) in 0.51s
以下是使用 order by 从句按 id’s 的升序来排列 customers 表中数据的一个示例。
[quickstart.cloudera:21000] > Select * from customers ORDER BY id asc;
执行后,上述查询将产生以下输出。
Query: select * from customers ORDER BY id asc
+----+----------+-----+-----------+--------+
| id | name | age | address | salary |
+----+----------+-----+-----------+--------+
| 1 | Ramesh | 32 | Ahmedabad | 20000 |
| 2 | Khilan | 25 | Delhi | 15000 |
| 3 | kaushik | 23 | Kota | 30000 |
| 4 | Chaitali | 25 | Mumbai | 35000 |
| 5 | Hardik | 27 | Bhopal | 40000 |
| 6 | Komal | 22 | MP | 32000 |
+----+----------+-----+-----------+--------+
Fetched 6 row(s) in 0.56s
同样,你可以使用 order by 从句按降序排列 customers 表中的数据,如下所示。
[quickstart.cloudera:21000] > Select * from customers ORDER BY id desc;
执行后,上述查询将产生以下输出。
Query: select * from customers ORDER BY id desc
+----+----------+-----+-----------+--------+
| id | name | age | address | salary |
+----+----------+-----+-----------+--------+
| 6 | Komal | 22 | MP | 32000 |
| 5 | Hardik | 27 | Bhopal | 40000 |
| 4 | Chaitali | 25 | Mumbai | 35000 |
| 3 | kaushik | 23 | Kota | 30000 |
| 2 | Khilan | 25 | Delhi | 15000 |
| 1 | Ramesh | 32 | Ahmedabad | 20000 |
+----+----------+-----+-----------+--------+
Fetched 6 row(s) in 0.54s