JOIN对于接触过数据库的人,这个词都不陌生,而且很多人很清楚各种JOIN,还有很多人对这个理解也不是很透彻。
假设我们有两个表,Table_A和Table_B。这两个表中的数据如下所示:
TABLE_A | TABLE_B PK Value | PK Value ---- ---------- | ---- ---------- 1 FOX | 1 TROT 2 COP | 2 CAR 3 TAXI | 3 CAB 6 WASHINGTON | 6 MONUMENT 7 DELL | 7 PC 5 ARIZONA | 8 MICROSOFT 4 LINCOLN | 9 APPLE 10 LUCENT | 11 SCOTCH
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] (as of Hive 0.10) table_reference: table_factor //表 | join_table //join语句 table_factor: tbl_name [alias] //表名[别名] | table_subquery alias //子查寻[别名] | ( table_references ) //带空号的table_refe<strong>本文来源gaodai#ma#com搞@代~码^网+</strong>rence join_condition: ON expression //on开头的条件语句
1、Inner JOIN: (内连接)
SELECT <select_list> FROM Table_A A LEFT JOIN Table_B B ON A.Key = B.Key WHERE B.Key IS NULL
-- Left Excluding JOIN SELECT A.PK AS A_PK, A.Value AS A_Value, B.Value AS B_Value, B.PK AS B_PK FROM Table_A A LEFT JOIN Table_B B ON A.PK = B.PK WHERE B.PK IS NULL A_PK A_Value B_Value B_PK ---- ---------- ---------- ---- 4 LINCOLN NULL NULL 5 ARIZONA NULL NULL 10 LUCENT NULL NULL (3 row(s) affected)