2016-09-20 20 views
0

确切的字符串值,我有2个表如何从分隔的列值匹配在Oracle数据库

Table 1: 
####### 
ID Location 
1 India 
2 Australia 

Table 2: 
############ 
Name Locations 
test1 India|North America 
test2 Indiana|Australia 

我用下面的查询从表2得到的名称,如果它包含位置在表2的位置。

select Name 
from table2 t2 inner join table1 t1 
    on instr(t1.Location,t2.Locations,length(t1.Location)) >= 1; 

但执行时仍然给我结果印第安纳州以及而它应该只是返回我一个人造成的位置印度。

我试过使用包含在查询中,但包含第二个参数作为字符串,但不作为列名称。

对此有没有其他方法?

回答

0

尝试查找的位置,分隔符,就像这样:

select Name from table2 t2 
inner join table1 t1 on instr(t2.Locations,t1.Location||'|') >= 1 
+0

这个工程,但如果要搜索的关键词是从分隔值哈钦斯“一.hutchings | a.hutch“它不应该返回值,但它也返回上面的字符串,我正在查找确切的字符串匹配 – user3493117

+0

如果'Location'substring将被放置在末尾,此查询无法正常工作'Locations'。看到它只是取代'印度|北美'到'北美|印度' – AlexSmet

+0

这个想法是正确的,但要解决评论中显示的问题,它需要进一步细化。你应该比较''|' || t1.Location || '|''到''|' || t2.Location || '|'' - 使用'instr()'或者更常见的'LIKE'比较运算符。 – mathguy

1

正则表达式总是帮助在这种情况下

with 
    table1 (id, location) as (
     select 1, 'India' from dual union 
     select 2, 'Australia' from dual 
    ), 
    table2 (name, locations) as (
     select 'test1', 'India|North America' from dual union 
     select 'test2', 'Indiana|Australia' from dual 
    ) 
select * 
from table2 join table1 on 
      regexp_like (locations, '(^|\|)' || location || '(\||$)') 
相关问题