Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » SQL Anywhere 服务器 - SQL 的用法 » 查询和修改数据 » 查询数据 » 全文搜索的类型

 

布尔搜索

在执行全文搜索时,可以指定由布尔运算符分隔的多个术语。在执行全文搜索时,SQL Anywhere 支持以下布尔运算符:AND、OR 和 AND NOT。

有关布尔搜索语法的详细信息,请参见CONTAINS 搜索条件

在全文搜索中使用 AND 运算符

使用 AND 运算符得到的匹配行是包含在 AND 两侧指定的两个术语的行。对于 AND 运算符,还可以使用和符号 (&)。如果指定的术语之间没有任何运算符,则暗指采用 AND 运算符。

术语的列出顺序并不重要。

例如,以下各个语句都可在 MarketingInformation.Description 中查找包含术语 fabric 和以 ski 开头的术语的行:

SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'ski* AND fabric' );
SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'fabric & ski*' );
SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'ski* fabric' );
在全文搜索中使用 OR 运算符

使用 OR 运算符得到的匹配行是至少包含在 OR 两侧指定的任一搜索术语的行。对于 OR 运算符,还可以使用竖线 (|);两者是等同的。

术语的列出顺序并不重要。

例如,以下任一语句都可返回 MarketingInformation.Description 中包含术语 fabric 或以 ski 开头的术语的行:

SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'ski* OR fabric' );
SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'fabric | ski*' );
在全文搜索中使用 AND NOT 运算符

使用 AND NOT 运算符查找的结果匹配左侧参数但不匹配右侧参数。对于 AND NOT 运算符,还可以使用连字符 (-);两者是等同的。

例如,以下语句互相等效,都返回包含术语 fabric 但不包含任何以 ski 开头的术语的行。

SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'fabric AND NOT ski*' );
SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'fabric -ski*' );
SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'fabric & -ski*' );
组合不同的布尔运算符

可在查询字符串中组合布尔运算符。例如,以下语句相互等效,都在 MarketingInformation.Description 列中搜索包含 fabricskin,但不包含 cotton 的项目:

SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'skin fabric -cotton' );
SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'fabric -cotton AND skin' );

以下语句相互等效,都在 MarketingInformation.Description 列中搜索包含 fabric,或同时包含 cottonskin 的项目:

SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'fabric | cotton AND skin' );
SELECT * 
   FROM MarketingInformation 
   WHERE CONTAINS ( MarketingInformation.Description, 'cotton skin OR fabric' );
对术语和短语分组

可以使用括号对术语和表达式分组。例如,以下语句在 MarketingInformation.Description 列中搜索包含 cottonfabric,并包含以 ski 开头的术语的项目。

SELECT ID, Description FROM MarketingInformation
   WHERE CONTAINS( MarketingInformation.Description, '( cotton OR fabric ) AND shi*' );
Description
902 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Tee Shirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>This simple, sleek, and lightweight technical shirt is designed for high-intensity workouts in hot and humid weather. The recycled polyester fabric is gentle on the earth and soft against your skin.</span></p></body></html>
903 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Tee Shirt</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>A sporty, casual shirt made of recycled water bottles. It will serve you equally well on trails or around town. The fabric has a wicking finish to pull perspiration away from your skin.</span></p></body></html>
906 <html><head><meta http-equiv=Content-Type content="text/html; charset=windows-1252"><title>Visor</title></head><body lang=EN-US><p><span style='font-size:10.0pt;font-family:Arial'>Lightweight 100% organically grown cotton construction. Shields against sun and precipitation. Metallic ions in the fibers inhibit bacterial growth, and help neutralize odor.</span></p></body></html>
在多列间执行搜索

可以通过单一查询在多个列之间执行全文搜索,前提是这些列是同一文本索引的一部分。

SELECT * 
   FROM Products 
      WHERE CONTAINS ( t.c1, t.c2, 'term1|term2' );
SELECT * 
   FROM t 
   WHERE CONTAINS( t.c1, 'term1' ) 
      OR CONTAINS( t.c2, 'term2' );

如果 t1.c1 包含 term1,或 t1.c2 包含 term2,则第一个查询匹配。

如果 t1.c1t1.c2 包含 term1term2 中的任一个,则第二个查询匹配。以这种方式使用 CONTAINS 子句也会返回匹配分数。请参见对全文搜索结果进行计分