如何在SQL连接中识别子集

时间:2019-08-01 23:34:33

标签: sql

我想在连接两个表时标识子集组。

我想确定下了哪个ProductGroup订单,条件是all option,Order中的值组合应与Product Options组中的相匹配。 [换句话说,订单的所有订单行都应该是产品组的子集]

产品选项表。

ProductKey| Option   | Value  | Group
PRD-A     | Shape    | Square |  1
PRA-A     | Color    | Blue   |  1
PRA-A     | Color    | Red    |  1  
PRD-A     | Round    | Square |  2
PRA-A     | Color    | Pink   |  2

订单表

OrderNo | ProductKey | Option | Value
ABCD    | PRD-A      | Shape  | Square
ABCD    | PRD-A      | Color  | Blue

我正在加入ProductKey,Option和Value。

我期待着。

ProductKey| Option   | Value  | Group
PRD-A     | Shape    | Square |  1
PRA-A     | Color    | Blue   |  1

但目前我也正在获取第2组记录。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

如果要获得预期的结果,则必须仅使用Option和Value联接表。

select p.* from Product p
inner join Order o
on  p.option = o.option
and p.value= o.value

答案 1 :(得分:0)

尝试一下:

 select p.ProductKey, p.option,p.value,p.group
 from product p , order o
 where p.ProductKey=o.ProductKey
 and p.option=o.option
 and p.value=o.value
 ;