我想找到一个允许匹配(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中两个表的连接条件中。
答案 0 :(得分:3)
您可以将regexp_substr
和regexp_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
P.S。我稍微改变了第三行。