我的要求是,我有两个表test_ref
和test_data
。
test_ref
表记录:
Pattern
--------------
Delhi
Chennai
Bangalore
Calcutta
Hyderabad
test_data
表recrods:
SlNo descrpt
--------------------------------------------------
1 Very code and hot in Delhi, is the north part of india
2 Calcutta is famous for fish
3 This is first correct message to return
4 Very hot and nice in Chennai city
5 green and clean city is the Bangalore
6 Kochi is famous for travel
7 This is second correct message to return
现在我必须从test_ref
表中搜索我的模式,其中descrpt
列来自表格test_data
,结果必须是
SlNo descrpt
--------------------------------------------------
3 This is first correct message to return
6 Kochi is famous for travel
7 This is second correct message to return
这里
Delhi
出现在test_data表的第一条记录中,因此应该消除。Calcutta
出现在test_data表的第三行,因此应该消除。请为此建议oracle查询。
答案 0 :(得分:1)
您不需要使用正则表达式:
Oracle 11g设置:
CREATE TABLE test_ref ( pattern ) AS
SELECT 'Delhi' FROM DUAL UNION ALL
SELECT 'Chennai' FROM DUAL UNION ALL
SELECT 'Bangalore' FROM DUAL UNION ALL
SELECT 'Calcutta' FROM DUAL UNION ALL
SELECT 'Hyderabad' FROM DUAL;
CREATE TABLE test_data ( SlNo, descrpt ) AS
SELECT 1, 'Very code and hot in Delhi, is the north part of india' FROM DUAL UNION ALL
SELECT 2, 'Calcutta is famous for fish' FROM DUAL UNION ALL
SELECT 3, 'This is first correct message to return' FROM DUAL UNION ALL
SELECT 4, 'Very hot and nice in Chennai city' FROM DUAL UNION ALL
SELECT 5, 'green and clean city is the Bangalore' FROM DUAL UNION ALL
SELECT 6, 'Kochi is famous for travel' FROM DUAL UNION ALL
SELECT 7, 'This is second correct message to return' FROM DUAL;
<强>查询1 强>:
SELECT *
FROM test_data d
WHERE NOT EXISTS ( SELECT 'X'
FROM test_ref r
WHERE INSTR( d.descrpt, r.Pattern ) > 0 );
<强>结果强>:
SLNO DESCRPT
---------- ------------------------------------------------------
3 This is first correct message to return
6 Kochi is famous for travel
7 This is second correct message to return
你也可以使用:
WHERE d.descrpt LIKE '%'||r.Pattern||'%'
或WHERE REGEXP_LIKE( d.descrpt, r.pattern )
(如果你真的想使用正则表达式)。答案 1 :(得分:0)
我不想用大数据集来做这件事,但以下工作:
with temp as
(
select 'Delhi Chennai Bangalore Calcutta Hyderabad' Pattern from dual
),
test_data as
( select 1 sino, 'Very code and hot in Delhi, is the north part of india' descrpt from dual
union all select 2 sino, 'Calcutta is famous for fish' descrpt from dual
union all select 3 sino, 'This is first correct message to return' descrpt from dual
union all select 4 sino, 'Very hot and nice in Chennai city' descrpt from dual
union all select 5 sino, 'green and clean city is the Bangalore' descrpt from dual
union all select 6 sino, 'Kochi is famous for travel' descrpt from dual
union all select 7 sino, 'This is second correct message to return' descrpt from dual)
select sino, descrpt
from test_data
where sino not in (select sino
from test_data t2
-- the next query breaks the pattern into individual words
,(select distinct
trim(regexp_substr(t.Pattern, '[^ ]+', 1, levels.column_value)) as word
from
temp t,
table(cast(multiset(select level from dual connect by level <= length (regexp_replace(t.pattern, '[^ ]+')) + 1) as sys.OdciNumberList)) levels) wrds
where t2.descrpt like '%'||wrds.word||'%')