匹配多个名字
Match multiple first names
我想找到一个允许匹配(SQL 连接)而不考虑顺序或完整字符串的正则表达式...如下:
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
etc...
这可能吗?我将在 Oracle SQL 中的两个表的连接条件中添加它。
您可以同时使用 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。我稍微改变了第三行。
我想找到一个允许匹配(SQL 连接)而不考虑顺序或完整字符串的正则表达式...如下:
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
etc...
这可能吗?我将在 Oracle SQL 中的两个表的连接条件中添加它。
您可以同时使用 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。我稍微改变了第三行。