如何使用两个不同的列

时间:2017-05-10 05:02:14

标签: sql oracle

我想查询表A中的列product_id和表B中的列ids。列ids包含针对不同产品的多个产品ID。例如,id中的单元格可以是'productA_id:21,productB_id:22 productC_id:43'。在这个长字符串中,不同的产品ID由','或''分隔。在我的例子中,产品A和B用','分开,产品B和产品C用''分开。

我想要做的是在表A中的product_id上连接这两个表等于表B的ids列中的productA_id。

例如,

表A:

enter image description here

表B:

enter image description here

在内部连接后,两个表的结果应为:

enter image description here

如何通过SQL查询获得结果?

**编辑:**现在我只能查询表格,这意味着我无法规范化任何事情。我必须接受表格,因为它们现在是什么。以前我使用此查询仅获取表B中的内容:

select * from tableB tt where exists 
(select 1 from (select distinct '%productA_id:'||product_id||' %' value from tableA) all_likes where tt.ids like all_likes.value) 
or exists 
(select 1 from (select distinct '%productA_id:'||product_id||',%' value from tableA) all_likes where tt.ids like all_likes.value) 
or exists
(select 1 from (select distinct '%productA_id:'||product_id value from tableA) all_likes where tt.ids like all_likes.value) 

但是,现在,我想从表A中获取信息。

1 个答案:

答案 0 :(得分:-3)

SELECT  a.prduct_id , a.value , b.id, b.ids, b.b_value 

FROM tableA a 

INNER JOIN tableB b ON a.prduct_id = b.id

但这很简单。你确定这就是全部吗?