[英]Oracle check if a string in column in table a contains word from column in table b
In Oracle 11.2.0.4... Given table A that has a long varchar column that has paragraphs of text, I want to check if any word in that text exists in a table that has a vertical list of singular words.在 Oracle 11.2.0.4 中......给定表 A 有一个包含文本段落的长 varchar 列,我想检查该文本中的任何单词是否存在于具有单字垂直列表的表中。 I am looking for a way to do this in a single query, not with cursors.我正在寻找一种在单个查询中执行此操作的方法,而不是使用游标。
Basically, I think I need to split the paragraph into words, then join on the table of vertical words, but am at a loss as to how to proceed.基本上,我认为我需要将段落拆分为单词,然后加入垂直单词表,但不知道如何进行。
Example:
TableA:
+----+--------------------------------------------+
| id | comments |
+----+--------------------------------------------+
| 1 | This sentence has no reference to any word |
| 2 | But this one references jungle |
| 3 | And this one references Trees |
+----+--------------------------------------------+
TableB:
+----+---------+
| id | word |
+----+---------+
| 1 | Jungle |
| 2 | Forest |
| 3 | Trees |
| 4 | Animals |
| 5 | River |
+----+---------+
Given those tables, I'd like a single SQL query that would tell me that Table A rows 2 and 3 have words that exist in Table B.给定这些表,我想要一个 SQL 查询,它会告诉我表 A 的第 2 行和第 3 行的单词存在于表 B 中。
I've looked into using the xmltype feature to split a table into words and then join those together like this:我研究过使用 xmltype 功能将表拆分为单词,然后像这样将它们连接在一起:
select id,
(select count(1) from tableb f, xmltable(
'/r/c/text()'
passing xmltype('<r><c>'||replace((regexp_replace(comments, '[^a-z][^A-Z]')), ' ', '</c><c>')||'</c></r>')
columns subs varchar2(4000) path '.'
) s where trim(f.word) = trim(s.subs)) words_found, comments
from tablea
where trim(comments) is not null
but that doesn't work very well.但这效果不佳。 It can only process a small set of comment lines before failing.它只能在失败之前处理一小组注释行。
Thanks in advance and I apologize if this has been answered.提前致谢,如果已经回答,我深表歉意。 I did quite a bit of checking and didn't find quite what I am looking for.我做了很多检查,并没有找到我正在寻找的东西。
Since you are only comparing the words, it's not required to split them.由于您只是比较单词,因此不需要拆分它们。 You may use a LIKE
expression in JOIN
您可以在JOIN
使用LIKE
表达式
select a.*, b.* from TableA a JOIN TableB b ON
' ' ||lower(a.comments)|| ' ' like '% '||lower(b.word)||' %'
OR REGEXP_LIKE
或REGEXP_LIKE
select a.*, b.* from TableA a JOIN TableB b ON
REGEXP_LIKE(a.comments,'(^|\W)'||b.word||'($|\W)','i');
How about a simple INSTR
?一个简单的INSTR
怎么样?
SQL> with
2 a (id, comments) as
3 (select 1, 'This sentence has no reference to any word' from dual union all
4 select 2, 'But this one references jungle' from dual union all
5 select 3, 'And this one references Trees' from dual union all
6 select 4, 'Jungle animals swim in a river' from dual
7 ),
8 b (id, word) as
9 (select 1, 'Jungle' from dual union all
10 select 2, 'Forest' from dual union all
11 select 3, 'Trees' from dual union all
12 select 4, 'Animals' from dual union all
13 select 5, 'River' from dual
14 )
15 select a.id, a.comments, b.word
16 from a cross join b
17 where instr(lower(a.comments), lower(b.word)) > 0
18 order by a.id, b.word;
ID COMMENTS WORD
---------- ------------------------------------------ -------
2 But this one references jungle Jungle
3 And this one references Trees Trees
4 Jungle animals swim in a river Animals
4 Jungle animals swim in a river Jungle
4 Jungle animals swim in a river River
SQL>
Can anyone improve @Jeff's solution to match only string from table A that start with the word from table B? 任何人都可以改进@Jeff的解决方案以仅匹配表A中以表B中的单词开头的字符串吗?
Example:
TableA:
+----+--------------------------------------------+
| id | comments |
+----+--------------------------------------------+
| 1 | This sentence has no reference to any word |
| 2 | But this one references jungle |
| 3 | Jungle this one references
+----+--------------------------------------------+
TableB:
+----+---------+
| id | word |
+----+---------+
| 1 | Jungle |
| 2 | Forest |
| 3 | Trees |
| 4 | Animals |
| 5 | River |
+----+---------+
Output:
3 Jungle this one references
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.