📅  最后修改于: 2020-11-30 05:05:48             🧑  作者: Mango
通常,选择查询的结果集中的行从0开始。使用offset子句,我们可以决定从何处考虑输出。例如,如果将偏移量选择为0,则结果将照常;如果将偏移量选择为5,则结果将从第五行开始。
以下是Impala中offset子句的语法。
select data from table_name Group BY col_name;
假设我们在数据库my_db中有一个名为客户的表,其内容如下-
[quickstart.cloudera:21000] > select * from customers;
Query: select * from customers
+----+----------+-----+-----------+--------+
| id | name | age | address | salary |
+----+----------+-----+-----------+--------+
| 3 | kaushik | 23 | Kota | 30000 |
| 6 | Komal | 22 | MP | 32000 |
| 1 | Ramesh | 32 | Ahmedabad | 20000 |
| 5 | Hardik | 27 | Bhopal | 40000 |
| 2 | Khilan | 25 | Delhi | 15000 |
| 8 | ram | 22 | vizag | 31000 |
| 9 | robert | 23 | banglore | 28000 |
| 7 | ram | 25 | chennai | 23000 |
| 4 | Chaitali | 25 | Mumbai | 35000 |
+----+----------+-----+-----------+--------+
Fetched 9 row(s) in 0.51s
您可以使用如下所示的limit和order by子句以ID的升序排列表中的记录,并将记录数限制为4。
Query: select * from customers order by id limit 4
+----+----------+-----+-----------+--------+
| 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 |
+----+----------+-----+-----------+--------+
Fetched 4 row(s) in 0.64s
以下是offset子句的示例。在这里,我们得到的客户表中的记录在自己的id的顺序和打印从第0行开始前四行。
[quickstart.cloudera:21000] > select * from customers order by id limit 4 offset 0;
执行时,上面的查询给出以下结果。
Query: select * from customers order by id limit 4 offset 0
+----+----------+-----+-----------+--------+
| 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 |
+----+----------+-----+-----------+--------+
Fetched 4 row(s) in 0.62s
以相同的方式,您可以从customers表中从偏移量为5的行开始获取四个记录,如下所示。
[quickstart.cloudera:21000] > select * from customers order by id limit 4 offset 5;
Query: select * from customers order by id limit 4 offset 5
+----+--------+-----+----------+--------+
| id | name | age | address | salary |
+----+--------+-----+----------+--------+
| 6 | Komal | 22 | MP | 32000 |
| 7 | ram | 25 | chennai | 23000 |
| 8 | ram | 22 | vizag | 31000 |
| 9 | robert | 23 | banglore | 28000 |
+----+--------+-----+----------+--------+
Fetched 4 row(s) in 0.52s