Heim  >  Artikel  >  Datenbank  >  Oracle 解决匹配的几种方法

Oracle 解决匹配的几种方法

WBOY
WBOYOriginal
2016-06-07 17:00:431094Durchsuche

//问题分析: //这是一个字符串匹配问题, //我们都知道,匹配就是在str1字符串中查找是否存在str2字符串, //并返回一个标

//数据  
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; 

linux

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Oracle 10g导入DMPNächster Artikel:Oracle的RAC环境下SYS密码修改