hive各种join关联表举例

文章目录

        • 两张测试表
        • full join
        • left join
        • right join
        • join即inner join
        • cross join
        • left semi join

两张测试表

select * from t1;
+--------+----------+
| t1.id  | t1.name  |
+--------+----------+
| 1      | a        |
| 2      | b        |
| 3      | c        |
+--------+----------+

select * from t2;
+-------+-------+
| t2.i  | t2.s  |
+-------+-------+
| 0     | x     |
| 2     | y     |
| 2     | yy    |
| 3     | z     |
+-------+-------+

full join

select * from t1 full join t2 on t1.id = t2.i;
+--------+----------+-------+-------+
| t1.id  | t1.name  | t2.i  | t2.s  |
+--------+----------+-------+-------+
| NULL   | NULL     | 0     | x     |
| 1      | a        | NULL  | NULL  |
| 2      | b        | 2     | y     |
| 2      | b        | 2     | yy    |
| 3      | c        | 3     | z     |
+--------+----------+-------+-------+

left join

select * from t1 left join t2 on t1.id = t2.i;
+--------+----------+-------+-------+
| t1.id  | t1.name  | t2.i  | t2.s  |
+--------+----------+-------+-------+
| 1      | a        | NULL  | NULL  |
| 2      | b        | 2     | y     |
| 2      | b        | 2     | yy    |
| 3      | c        | 3     | z     |
+--------+----------+-------+-------+

right join

select * from t1 right join t2 on t1.id = t2.i;
+--------+----------+-------+-------+
| t1.id  | t1.name  | t2.i  | t2.s  |
+--------+----------+-------+-------+
| NULL   | NULL     | 0     | x     |
| 2      | b        | 2     | y     |
| 2      | b        | 2     | yy    |
| 3      | c        | 3     | z     |
+--------+----------+-------+-------+

join即inner join

select * from t1 inner join t2 on t1.id = t2.i;
+--------+----------+-------+-------+
| t1.id  | t1.name  | t2.i  | t2.s  |
+--------+----------+-------+-------+
| 2      | b        | 2     | y     |
| 2      | b        | 2     | yy    |
| 3      | c        | 3     | z     |
+--------+----------+-------+-------+

select * from t1 join t2 on t1.id = t2.i;
+--------+----------+-------+-------+
| t1.id  | t1.name  | t2.i  | t2.s  |
+--------+----------+-------+-------+
| 2      | b        | 2     | y     |
| 2      | b        | 2     | yy    |
| 3      | c        | 3     | z     |
+--------+----------+-------+-------+

cross join

select * from t1 cross join t2 on t1.id = t2.i;
+--------+----------+-------+-------+
| t1.id  | t1.name  | t2.i  | t2.s  |
+--------+----------+-------+-------+
| 2      | b        | 2     | y     |
| 2      | b        | 2     | yy    |
| 3      | c        | 3     | z     |
+--------+----------+-------+-------+

select * from t1 cross join t2;
+--------+----------+-------+-------+
| t1.id  | t1.name  | t2.i  | t2.s  |
+--------+----------+-------+-------+
| 1      | a        | 0     | x     |
| 3      | c        | 0     | x     |
| 2      | b        | 0     | x     |
| 1      | a        | 2     | y     |
| 3      | c        | 2     | y     |
| 2      | b        | 2     | y     |
| 1      | a        | 2     | yy    |
| 3      | c        | 2     | yy    |
| 2      | b        | 2     | yy    |
| 1      | a        | 3     | z     |
| 3      | c        | 3     | z     |
| 2      | b        | 3     | z     |
+--------+----------+-------+-------+

select * from t1, t2;
+--------+----------+-------+-------+
| t1.id  | t1.name  | t2.i  | t2.s  |
+--------+----------+-------+-------+
| 1      | a        | 0     | x     |
| 3      | c        | 0     | x     |
| 2      | b        | 0     | x     |
| 1      | a        | 2     | y     |
| 3      | c        | 2     | y     |
| 2      | b        | 2     | y     |
| 1      | a        | 2     | yy    |
| 3      | c        | 2     | yy    |
| 2      | b        | 2     | yy    |
| 1      | a        | 3     | z     |
| 3      | c        | 3     | z     |
| 2      | b        | 3     | z     |
+--------+----------+-------+-------+

select * from t1, t2 where t1.id = t2.i;
+--------+----------+-------+-------+
| t1.id  | t1.name  | t2.i  | t2.s  |
+--------+----------+-------+-------+
| 2      | b        | 2     | y     |
| 2      | b        | 2     | yy    |
| 3      | c        | 3     | z     |
+--------+----------+-------+-------+

left semi join

select * from t1 left semi join t2 on t1.id = t2.i;
+--------+----------+
| t1.id  | t1.name  |
+--------+----------+
| 2      | b        |
| 3      | c        |
+--------+----------+

select * from t2 left semi join t1 on t1.id = t2.i;
+-------+-------+
| t2.i  | t2.s  |
+-------+-------+
| 2     | y     |
| 2     | yy    |
| 3     | z     |
+-------+-------+

select * from t1 left semi join t2 on t1.id = t2.i and t1.name = 'b';
+--------+----------+
| t1.id  | t1.name  |
+--------+----------+
| 2      | b        |
+--------+----------+

select * from t1 left semi join t2 on t1.id = t2.i and t2.s = 'z';
+--------+----------+
| t1.id  | t1.name  |
+--------+----------+
| 3      | c        |
+--------+----------+

select * from t1 left semi join t2 on t1.id = t2.i and t2.s = 'yy';
+--------+----------+
| t1.id  | t1.name  |
+--------+----------+
| 2      | b        |
+--------+----------+

你可能感兴趣的:(大数据)