简体   繁体   English

Oracle查询中字符串中存在3个单词

[英]Existance of 3 words in a string in oracle query

I want to check whether 3 specific words exists in a column or not using an Oracle query. 我想使用Oracle查询来检查一列中是否存在3个特定的单词。

For example my column value is: 'Google Earth lets you fly anywhere on Earth to view satellite imagery, maps, terrain, 3D buildings, from galaxies in outer space to the canyons of the ocean' . 例如,我的列值是: 'Google Earth lets you fly anywhere on Earth to view satellite imagery, maps, terrain, 3D buildings, from galaxies in outer space to the canyons of the ocean'

I want to check whether the three words Earth , galaxies and buildings exist in the string. 我想检查字符串中是否存在Earthgalaxiesbuildings这三个词。

How I can do this in an Oracle query? 如何在Oracle查询中做到这一点?

You want to look for words only probably. 您可能只想查找单词。 So when looking for 'space' you don't want to find, say, 'respaced' . 因此,当寻找'space'您不想找到'respaced' Use REGEXP_LIKE with word boundaries: REGEXP_LIKE与单词边界一起使用:

select *
from mytable 
where regexp_like(text, '(^|\W)earth(\W|$)', 'i')
  and regexp_like(text, '(^|\W)galaxies(\W|$)', 'i')
  and regexp_like(text, '(^|\W)buildings(\W|$)', 'i');

Use something like this in the where clause (if you want to be exact about the case): 在where子句中使用类似这样的内容(如果您想确切了解这种情况):

where col_name like '%Earth%' 
and col_name like '%galaxies%' 
and col_name like '%buildings%'

as @Tim pointed out in the comments, if you want to ignore case, you can by using upper() or lower(): 正如@Tim在注释中指出的那样,如果要忽略大小写,可以使用upper()或lower():

where upper(col_name) like '%EARTH%'
and upper(col_name) like '%GALAXIES%'

etc. 等等

Use regexp: 使用正则表达式:

WITH tmp AS
  (
    SELECT 'Earth, galaxies and buildings' str FROM dual UNION ALL
    SELECT 'Earth, buildings and galaxies' str FROM dual UNION ALL
    SELECT 'Earth2, galaxies and buildings' str FROM dual UNION ALL
    SELECT 'Earth , galaxies and buildings' str FROM dual UNION ALL
    SELECT 'Earth,galaxies,buildings' str FROM dual UNION ALL
    SELECT 'Earthgalaxiesbuildings' str FROM dual UNION ALL
    SELECT 'earth, galaxies and buildings' str FROM dual
  )
SELECT
  str
FROM
  tmp
WHERE
  REGEXP_LIKE(UPPER(str), '([[:punct:][:space:]]|^)EARTH([[:punct:][:space:]]|$)') AND
  REGEXP_LIKE(UPPER(str), '([[:punct:][:space:]]|^)GALAXIES([[:punct:][:space:]]|$)') AND
  REGEXP_LIKE(UPPER(str), '([[:punct:][:space:]]|^)BUILDINGS([[:punct:][:space:]]|$)')

"Earth" "Earth," should be selected as word as per logic. 应根据逻辑选择“地球”“地球”作为单词。 Using '%Earth% will also become true for words like "Un-Earth" or "Earthing" and you don't want it. 对于“ Un-Earth”或“ Earthing”之类的词,使用'%Earth%也将变为true,而您不希望如此。

So, 所以,

where (upper(col) like upper('% earth %') OR upper(col) like upper('% earth.%') OR upper(col) like upper('% earth,%') ) AND
  (upper(col) like upper('% galaxies %') OR upper(col) like upper('% galaxies.%') OR upper(col) like upper('% galaxies,%')) AND
  upper(col) like upper('% buildings %') OR upper(col) like upper('% buildings.%') OR upper(col) like upper('% buildings,%'))

based on how much data is corrupt, you can add multiple condition inside OR. 根据损坏的数据量,您可以在OR中添加多个条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM