如何检查列的值是否在另一个查询提供的有效列表中

时间:2018-02-15 11:33:48

标签: sql hive

我有两个问题,第一个给我以下信息:

SELECT
     x.id_receipt,
     x.id_employee,
     x.product_id,
     x.receipt_date,
     bb.product_name,
from receipt x 
Left join product bb on x.id_product=bb.id

下一个是:

 SELECT

     a.id,
     v.allowed_product

from employee a 
Left join valid_product v on a.id=v.id_employee

对于每位员工,都有一份有效产品清单。 如果每次购物都是允许购买员工,我该如何检查第一个查询?

我想检查bb.product_name是否在第二个查询中每个员工的允许产品列表中

2 个答案:

答案 0 :(得分:0)

我认为您可以使用其他join

来执行此操作
select x.id_receipt, x.id_employee, x.product_id, x.receipt_date,
       bb.product_name,
from receipt x join
     product bb
     on x.id_product = bb.id join
     valid_product vp
     on vp.id_employee = x.id_employee and vp.allowed_product = bb.id;

由于产品必须匹配,因此无需left join

答案 1 :(得分:0)

I think you can use the WHERE clause to specify what you are looking for. Like:

SELECT bb.product_name
FROM receipt as x, employee as a
WHERE x.bb.product_name = a.allowed_product