Impala - Union Clause

您可以使用Impala的 Union 子句合并两个查询的结果.

语法

以下是Impala中 Union 子句的语法.

query1 union query2;

示例

假设我们在数据库中有一个名为 customers 的表 my_db 及其内容如下 :

[quickstart.cloudera:21000] > select * from customers; Query: select * from customers 
+----+----------+-----+-----------+--------+ 
| id | name     | age | address   | salary | 
+----+----------+-----+-----------+--------+ 
| 1  | Ramesh   | 32  | Ahmedabad | 20000  | 
| 9  | robert   | 23  | banglore  | 28000  | 
| 2  | Khilan   | 25  | Delhi     | 15000  | 
| 4  | Chaitali | 25  | Mumbai    | 35000  | 
| 7  | ram      | 25  | chennai   | 23000  | 
| 6  | Komal    | 22  | MP        | 32000  | 
| 8  | ram      | 22  | vizag     | 31000  | 
| 5  | Hardik   | 27  | Bhopal    | 40000  | 
| 3  | kaushik  | 23  | Kota      | 30000  | 
+----+----------+-----+-----------+--------+ 
Fetched 9 row(s) in 0.59s

以同样的方式,假设我们有另一个名为 employee 的表及其内容如下 :

[quickstart.cloudera:21000] > select * from employee; Query: select * from employee 
+----+---------+-----+---------+--------+ 
| id | name    | age | address | salary | 
+----+---------+-----+---------+--------+ 
| 3  | mahesh  | 54  | Chennai | 55000  | 
| 2  | ramesh  | 44  | Chennai | 50000  | 
| 4  | Rupesh  | 64  | Delhi   | 60000  | 
| 1  | subhash | 34  | Delhi   | 40000  | 
+----+---------+-----+---------+--------+ 
Fetched 4 row(s) in 0.59s

以下是Impala中 union 子句的示例.在这个例子中,我们按照id的顺序排列两个表中的记录,并使用两个单独的查询将它们的数量限制为3,并使用 UNION 子句加入这些查询.

[quickstart.cloudera:21000] > select * from customers order by id limit 3
 union select * from employee order by id limit 3;

执行时,上面的查询给出以下输出.

Query: select * from customers order by id limit 3 union select 
   * from employee order by id limit 3 
+----+---------+-----+-----------+--------+ 
| id | name    | age | address   | salary | 
+----+---------+-----+-----------+--------+ 
| 2  | Khilan  | 25  | Delhi     | 15000  |
| 3  | mahesh  | 54  | Chennai   | 55000  | 
| 1  | subhash | 34  | Delhi     | 40000  | 
| 2  | ramesh  | 44  | Chennai   | 50000  | 
| 3  | kaushik | 23  | Kota      | 30000  | 
| 1  | Ramesh  | 32  | Ahmedabad | 20000  | 
+----+---------+-----+-----------+--------+ 
Fetched 6 row(s) in 3.11s

中获取6行