匹配多个名字

问题描述 投票:2回答:1

我想找到一个允许匹配(SQL连接)而不考虑订单或完整字符串的RegEx ...如下:

Case 1
Left: Mr. John Doe
Right: Doe John
Result: match

Case 2
Left: John Doe
Right Doe
Result: match

Case 3
Left: Robert-John Doe
Right: Robert Doe
Result: match

等等...

这可能吗?我将在Oracle SQL中的两个表的连接条件中添加它。

sql oracle oracle11g oracle10g
1个回答
3
投票

您可以将regexp_substrregexp_count一起使用:

with t( id, col1, col2 ) as
(
 select 1, 'Mr. John Doe'   , 'Doe John'   from dual union all
 select 2, 'John Doe'       , 'Doe'        from dual union all
 select 3, 'Robert-John Doe', 'Robert Do'  from dual        
), t2 as
(
select distinct t.*, 
       regexp_substr(col1, '[^ ]+', 1, level) as col01, 
       regexp_substr(col2, '[^ ]+', 1, level) as col02,
       level                
  from dual 
 cross join t
connect by level <= greatest(regexp_count(col1, '[^ ]+'),regexp_count(col2, '[^ ]+'))  
 order by id, level
 )
select distinct id, col1, col2, 'matched' as status
  from t2 t
 where exists ( select 1 from t2 where id = t.id and ( col01 = t.col02 or col02 = t.col01 ) )
 union all
select distinct id, col1, col2, 'Not matched'
  from t2 t
 where not exists ( select 1 from t2 where id = t.id and ( col01 = t.col02 or col02 = t.col01 ) )
 order by id;

ID  COL1            COL2        STATUS
--  --------------- ----------  -----------
1   Mr. John Doe    Doe John    matched
2   John Doe        Doe         matched
3   Robert-John Doe Robert Do   Not matched

Demo

附:我稍微改变了第三排。

© www.soinside.com 2019 - 2024. All rights reserved.