Hive(十五)join

    hive join 主要包括join(内连接)、left join(左连接)、right join(右连接)、full join(全连接)、left semi join(左半连接)、map side join(map端连接)六种用法,其中inner join 和 join等价,left outer join 和 left join等价,right outer join 和 right join 等价,full outer join 和 full join等价。 正确选择合适的join 类型在开发过程中可以提升效率。在Hive中,JOIN操作用于将两个或多个表中的数据连接在一起,以便进行联合查询和分析。在连接查询的时候,如果不指定,那么默认使用的是inner join在Hive中,除了支持上述比较常用的join以外,还支持left semi join。当a left semi join b的时候,表示获取a表中的数据哪些在b表中出现过

在hdfs上创建两个目录orders和products

order.txt

2001 20240710 3 21
2002 20240710 4 1001
2003 20240710 3 401
2004 20240711 2 14
2005 20240711 2 45
2006 20240823 4 10
2007 20240823 2 31
2008 20240823 3 23
2009 20240912 2 10
2010 20240912 2 21
2011 20240914 3 17
2012 20240914 3 19

product.txt

1 one 4999
2 two 6999
3 three 6999
4 four 7999

use demo;

create external table products (productid int, name string, price double) row format delimited fields terminated by ' ' location '/userorders/products';

 create external table orders (orderid int, orderdate string,productid int, nm int) row format delimited fields terminated by ' ' location '/userorders/orders';

右连接-以右表为准:

select * from orders o right join products p on o.productid = p.productid;

左连接-以左表为准

select * from orders o left join products p on o.productid = p.productid;

 内连接 - 获取两个表都有的数据:

select * from orders o inner join products p on o.productid = p.productid;

1:查询被卖出去过的商品 - 获取商品表中的哪些数据在订单表中出现过

select * from products p left semi join orders o on p.productid = o.productid;

2:获取每天卖了多少钱:

select o.orderdate, sum(o.nm*p.price) from orders o inner join products p on o.productid = p.productid group by o.orderdate;

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allen019

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值