//資料
with ta as(
select 1 id,'f1,f2' fid from dual union all
select 2,'f4,f5,f6' from dual)
,tb as(
select 'f1' fid,'name1' fname from dual union all
select 'f2','name2' from dual union all
select 'f3','name3' from dual union all
select 'f4','name4' from dual union all
select 'f5','name5' from dual union all
select 'f6','name6' from dual)
//結果:
ID FID FNAME
---------- -------- --------------------------------------------
1 f1,f2 name1,name2
2 f4,f5,f6 name4,name6,name5
//問題分析:
//這是一個字元串比對問題,
//我們都知道,比對就是在str1字元串中查找是否存在str2字元串,
//并傳回一個辨別,标記str2是否存在于str1裡面;
//oracle提供了一些函數供我們實作比對功能:
//字元函數instr,contains,正規表達式函數regexp_instr;
//我們還可以通過like模糊比對查詢。
//是以上面問題的解決方法有下面幾種,
//如果您還能想到更好的方法,請賜教!謝謝!
//方法一
select ta.id id,
ta.fid fid,
wm_concat(tb.fname) fname
from ta,tb
where instr(ta.fid,tb.fid)>0
group by ta.id,ta.fid;
--
//方法二
select ta.id id,
ta.fid fid,
wm_concat(tb.fname) fname
from ta,tb
where ta.fid like '%'||tb.fid||'%'
group by ta.id,ta.fid;
--
//方法三
select ta.id id,
ta.fid fid,
wm_concat(tb.fname) fname
from ta,tb
where regexp_instr(ta.fid,tb.fid)>0
group by ta.id,ta.fid;