2016-09-20 57 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 || '(\||$)')