oracle查询

时间:2017-01-30 14:32:50

标签: sql oracle

我想使用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查询中执行此操作?

4 个答案:

答案 0 :(得分:4)

你想要查找单词。因此,在寻找'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');

答案 1 :(得分:0)

在where子句中使用类似的东西(如果你想确切地说明这种情况):

it('should be able to play audio',() => {
    const audio = document.createElement('audio');

    expect.spyOn(document, 'createElement').andReturn(audio);
    expect.spyOn(audio, 'canPlayType').andReturn(true);

    expect(canPlayMedia()).toBe(true);

    document.createElement.restore();
});
@Tim在评论中指出,如果你想忽略大小写,可以使用upper()或lower():

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

“地球”“地球”应根据逻辑选择为单词。使用'%Earth%'对于像“Un-Earth”或“Earthing”这样的词也会成为现实,你不需要它。

所以,

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中添加多个条件。