【问题标题】:How does contains() in PL-SQL work?PL-SQL 中的 contains() 是如何工作的?
【发布时间】:2011-01-26 17:26:23
【问题描述】:

在我的查询中使用 contains() 方法有很多不必要的结果。不要告诉我使用 like 或其他东西。它是硬编码的,无法更改。

【问题讨论】:

  • 我可以将任何参数传递给包含以将搜索结果缩小到精确结果
  • Select * from blabla where contains(dFullText, "car")

标签: sql oracle oracle10g contains


【解决方案1】:

请参阅oracle.com 中的此示例

declare 
rowno number := 0; 
   begin 
   for c1 in (SELECT SCORE(1) score, title FROM news 
          WHERE CONTAINS(text, 'oracle', 1) > 0
          ORDER BY SCORE(1) DESC) 
   loop 
   rowno := rowno + 1; 
   dbms_output.put_line(c1.title||': '||c1.score); 
exit when rowno = 10; 
end loop; 
end; 

【讨论】:

    【解决方案2】:

    包含用于具有“上下文索引”的文本字段,该索引为文本字段编制索引以进行搜索。标准用法是这样的(使用score 运算符,根据contains 中的1 与score 中的1 匹配,显示从contains 子句返回的内容):

    SELECT score(1), value
    FROM table_name
    WHERE CONTAINS(textField, 'searchString', 1) > 0;
    

    对于表table_name中这样的数据

    value  |  textField
    -------|-----------------------------------------------
    A      |   'Here is searchString.  searchString again.'
    B      |   'Another string'
    C      |   'Just one searchString'
    

    该查询将返回

    2 A
    1 C
    

    所以 contains 类似于 like,但会计算一个字符串在文本字段中出现的次数。我找不到使用包含在您发布的查询中使用它的方式的资源,但我认为这将返回其中dFullText 至少有一个car 实例的行,或者相当于这个sql:

    Select * from blabla where dFullText like "%car%"
    

    Here 是另一个来源。

    【讨论】:

    • contains() 和 instr() 有什么区别吗?
    • @Rene,是的,它们是不同的。 Contains() 计算字符串在文本字段中出现的次数并返回该数字。 instr() 在字段(或其他字符串)中搜索字符串并返回找到的第一个搜索字符串的索引(如果未找到,则返回 0)。查看instr() 的示例techonthenet.com/oracle/functions/instr.php
    猜你喜欢
    • 1970-01-01
    • 2015-10-31
    • 2016-10-20
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-15
    • 1970-01-01
    相关资源
    最近更新 更多