HiveQL - 选择连接



JOIN 是一个用于通过使用每个表共有的值组合两个表的特定字段的子句。它用于组合数据库中两个或多个表中的记录。

语法

join_table:

   table_reference JOIN table_factor [join_condition]
   | table_reference {LEFT|RIGHT|FULL} [OUTER] JOIN table_reference
   join_condition
   | table_reference LEFT SEMI JOIN table_reference join_condition
   | table_reference CROSS JOIN table_reference [join_condition]

示例

我们将在本章中使用以下两个表。考虑以下名为 CUSTOMERS 的表。

+----+----------+-----+-----------+----------+ 
| ID | NAME     | AGE | ADDRESS   | SALARY   | 
+----+----------+-----+-----------+----------+ 
| 1  | Ramesh   | 32  | Ahmedabad | 2000.00  |  
| 2  | Khilan   | 25  | Delhi     | 1500.00  |  
| 3  | kaushik  | 23  | Kota      | 2000.00  | 
| 4  | Chaitali | 25  | Mumbai    | 6500.00  | 
| 5  | Hardik   | 27  | Bhopal    | 8500.00  | 
| 6  | Komal    | 22  | MP        | 4500.00  | 
| 7  | Muffy    | 24  | Indore    | 10000.00 | 
+----+----------+-----+-----------+----------+

考虑另一个名为 ORDERS 的表,如下所示

+-----+---------------------+-------------+--------+ 
|OID  | DATE                | CUSTOMER_ID | AMOUNT | 
+-----+---------------------+-------------+--------+ 
| 102 | 2009-10-08 00:00:00 |           3 | 3000   | 
| 100 | 2009-10-08 00:00:00 |           3 | 1500   | 
| 101 | 2009-11-20 00:00:00 |           2 | 1560   | 
| 103 | 2008-05-20 00:00:00 |           4 | 2060   | 
+-----+---------------------+-------------+--------+

连接类型如下所示

  • JOIN
  • 左外连接 (LEFT OUTER JOIN)
  • 右外连接 (RIGHT OUTER JOIN)
  • 全外连接 (FULL OUTER JOIN)

JOIN

JOIN 子句用于组合和检索来自多个表的记录。JOIN 在 SQL 中与 OUTER JOIN 相同。JOIN 条件需要使用表的主键和外键来提出。

以下查询在 CUSTOMER 和 ORDER 表上执行 JOIN,并检索记录

hive> SELECT c.ID, c.NAME, c.AGE, o.AMOUNT 
FROM CUSTOMERS c JOIN ORDERS o 
ON (c.ID = o.CUSTOMER_ID);

查询成功执行后,您将看到以下响应

+----+----------+-----+--------+ 
| ID | NAME     | AGE | AMOUNT | 
+----+----------+-----+--------+ 
| 3  | kaushik  | 23  | 3000   | 
| 3  | kaushik  | 23  | 1500   | 
| 2  | Khilan   | 25  | 1560   | 
| 4  | Chaitali | 25  | 2060   | 
+----+----------+-----+--------+

左外连接 (LEFT OUTER JOIN)

HiveQL 左外连接 (LEFT OUTER JOIN) 返回左表中的所有行,即使右表中没有匹配项。这意味着,如果 ON 子句在右表中匹配 0(零)条记录,则 JOIN 仍然在结果中返回一行,但在右表中的每一列中都为 NULL。

左连接 (LEFT JOIN) 返回左表中的所有值,加上右表中匹配的值,或者在没有匹配的 JOIN 条件的情况下为 NULL。

以下查询演示了 CUSTOMER 和 ORDER 表之间的左外连接 (LEFT OUTER JOIN)

hive> SELECT c.ID, c.NAME, o.AMOUNT, o.DATE 
FROM CUSTOMERS c 
LEFT OUTER JOIN ORDERS o 
ON (c.ID = o.CUSTOMER_ID);

查询成功执行后,您将看到以下响应

+----+----------+--------+---------------------+ 
| ID | NAME     | AMOUNT | DATE                | 
+----+----------+--------+---------------------+ 
| 1  | Ramesh   | NULL   | NULL                | 
| 2  | Khilan   | 1560   | 2009-11-20 00:00:00 | 
| 3  | kaushik  | 3000   | 2009-10-08 00:00:00 | 
| 3  | kaushik  | 1500   | 2009-10-08 00:00:00 | 
| 4  | Chaitali | 2060   | 2008-05-20 00:00:00 | 
| 5  | Hardik   | NULL   | NULL                | 
| 6  | Komal    | NULL   | NULL                | 
| 7  | Muffy    | NULL   | NULL                | 
+----+----------+--------+---------------------+

右外连接 (RIGHT OUTER JOIN)

HiveQL 右外连接 (RIGHT OUTER JOIN) 返回右表中的所有行,即使左表中没有匹配项。如果 ON 子句在左表中匹配 0(零)条记录,则 JOIN 仍然在结果中返回一行,但在左表中的每一列中都为 NULL。

右连接 (RIGHT JOIN) 返回右表中的所有值,加上左表中匹配的值,或者在没有匹配的连接条件的情况下为 NULL。

以下查询演示了 CUSTOMER 和 ORDER 表之间的右外连接 (RIGHT OUTER JOIN)。

notranslate"> hive> SELECT c.ID, c.NAME, o.AMOUNT, o.DATE FROM CUSTOMERS c RIGHT OUTER JOIN ORDERS o ON (c.ID = o.CUSTOMER_ID);

查询成功执行后,您将看到以下响应

+------+----------+--------+---------------------+ 
| ID   | NAME     | AMOUNT | DATE                | 
+------+----------+--------+---------------------+ 
| 3    | kaushik  | 3000   | 2009-10-08 00:00:00 | 
| 3    | kaushik  | 1500   | 2009-10-08 00:00:00 | 
| 2    | Khilan   | 1560   | 2009-11-20 00:00:00 | 
| 4    | Chaitali | 2060   | 2008-05-20 00:00:00 | 
+------+----------+--------+---------------------+

全外连接 (FULL OUTER JOIN)

HiveQL 全外连接 (FULL OUTER JOIN) 组合满足 JOIN 条件的左右两个外表的记录。连接后的表包含两个表的所有记录,或者在任一侧缺少匹配项时填充 NULL 值。

以下查询演示了 CUSTOMER 和 ORDER 表之间的全外连接 (FULL OUTER JOIN)

hive> SELECT c.ID, c.NAME, o.AMOUNT, o.DATE 
FROM CUSTOMERS c 
FULL OUTER JOIN ORDERS o 
ON (c.ID = o.CUSTOMER_ID);

查询成功执行后,您将看到以下响应

+------+----------+--------+---------------------+ 
| ID   | NAME     | AMOUNT | DATE                | 
+------+----------+--------+---------------------+ 
| 1    | Ramesh   | NULL   | NULL                | 
| 2    | Khilan   | 1560   | 2009-11-20 00:00:00 | 
| 3    | kaushik  | 3000   | 2009-10-08 00:00:00 | 
| 3    | kaushik  | 1500   | 2009-10-08 00:00:00 | 
| 4    | Chaitali | 2060   | 2008-05-20 00:00:00 | 
| 5    | Hardik   | NULL   | NULL                | 
| 6    | Komal    | NULL   | NULL                |
| 7    | Muffy    | NULL   | NULL                |  
| 3    | kaushik  | 3000   | 2009-10-08 00:00:00 | 
| 3    | kaushik  | 1500   | 2009-10-08 00:00:00 | 
| 2    | Khilan   | 1560   | 2009-11-20 00:00:00 | 
| 4    | Chaitali | 2060   | 2008-05-20 00:00:00 | 
+------+----------+--------+---------------------+
广告