繁体   English   中英

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

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

我想使用Oracle查询来检查一列中是否存在3个特定的单词。

例如,我的列值是: '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'

我想检查字符串中是否存在Earthgalaxiesbuildings这三个词。

如何在Oracle查询中做到这一点?

您可能只想查找单词。 因此,当寻找'space'您不想找到'respaced' 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');

在where子句中使用类似这样的内容(如果您想确切了解这种情况):

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

正如@Tim在注释中指出的那样,如果要忽略大小写,可以使用upper()或lower():

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

等等

使用正则表达式:

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:]]|$)')

应根据逻辑选择“地球”“地球”作为单词。 对于“ Un-Earth”或“ Earthing”之类的词,使用'%Earth%也将变为true,而您不希望如此。

所以,

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,%'))

根据损坏的数据量,您可以在OR中添加多个条件。

暂无
暂无

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

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