简体   繁体   English

Oracle 检查表 a 中列中的字符串是否包含表 b 中列中的单词

[英]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_LIKEREGEXP_LIKE

select a.*, b.* from TableA a  JOIN TableB b  ON 
REGEXP_LIKE(a.comments,'(^|\W)'||b.word||'($|\W)','i');

Demo 演示

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.

相关问题 如果列表包含表b另一列的子字符串,则用表b的列值更新列表 - update column table with value of column from table b if it contains substring from another column from table b 检查列表A中的一部分字符串是否存在于另一个列表B中 - Check if part of string in a column table A exists in another column table B 如何检查/选择字符串是否包含表列值中的值? - How to check/select if a string contains value from a table column values? SQL:将表 B 中的列添加到表 A - SQL: Adding column from table B to table A 从表B到表A的MySQL更新列 - MySQL update column from table B to table A SQL - 如果一个表中的列中的字符串包含来自连接表的列中的字符串 - SQL - If string in a column from one table contains string in column from joined table 如何检查表是否包含特定的列? - How to check whether the table contains a particular column or not? SQL:检查表是否在特定列中不包含任何空值 - SQL: check if a table contains no nulls in a particular column 如果表A中的列A与表B中的列B相等,则从两个表(表A和b)中获取输出 - get output from two tables(table A and b) if column A from table A is equal to column B from Table B 如果&#39;table a&#39;。&#39;column b&#39;与&#39;table b&#39;。&#39;column a&#39;相匹配,如何用&#39;table b&#39;。&#39;column b&#39;替换&#39;table a&#39;。&#39;column b&#39; - how to replace 'table a'.'column b' with 'table b'.'column b' if 'table a'.'column b' matches 'table b'.'column a'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM