

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# LIKE
<a name="patternmatching_condition_like"></a>

LIKE 运算符将字符串表达式（如列名称）与使用通配符 %（百分比）和 \$1（下划线）的模式进行比较。LIKE 模式匹配始终涵盖整个字符串。若要匹配字符串中任意位置的序列，模式必须以百分比符号开始和结尾。

LIKE 区分大小写。

## 语法
<a name="patternmatching_condition_like-synopsis"></a>

```
expression [ NOT ] LIKE | pattern [ ESCAPE 'escape_char' ]
```

## 参数
<a name="patternmatching_condition_like-arguments"></a>

 *expression*   
有效的 UTF-8 字符表达式（如列名称）。

LIKE  
LIKE 执行区分大小写的模式匹配。要为多字节字符执行不区分大小写的模式匹配，请将 *expression* 上的 [LOWER](LOWER.md) 函数和带有 LIKE 函数的 *pattern* 一起使用。  
与比较谓词（例如 = 和 <>）相比，LIKE 谓词不会隐式忽略尾随空格。要忽略尾随空格，请使用 RTRIM 或者将 CHAR 列显式强制转换为 VARCHAR。  
该`~~`运算符等同于 LIKE。此外，`!~~`运算符等同于 NOT LIKE。

 *pattern*   
具有要匹配的模式的有效的 UTF-8 字符表达式。

 *escape\$1char*   
将对模式中的元字符进行转义的字符表达式。默认为两个反斜杠 ('\$1\$1')。

如果 *pattern* 不包含元字符，则模式仅表示字符串本身；在此情况下，LIKE 的行为与等于运算符相同。

其中一个字符表达式可以是 CHAR 或 VARCHAR 数据类型。如果它们不同， AWS Clean Rooms 会将 *pattern* 转换为 *expression* 的数据类型。

LIKE 支持下列模式匹配元字符：

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/clean-rooms/latest/sql-reference/patternmatching_condition_like.html)

## 示例
<a name="patternmatching_condition_like-examples"></a>

下表显示使用 LIKE 的模式匹配的示例：

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/clean-rooms/latest/sql-reference/patternmatching_condition_like.html)

以下示例查找名称以“E”开头的所有城市：

```
select distinct city from users
where city like 'E%' order by city;
city
---------------
East Hartford
East Lansing
East Rutherford
East St. Louis
Easthampton
Easton
Eatontown
Eau Claire
...
```

以下示例查找姓中包含“ten”的用户：

```
select distinct lastname from users
where lastname like '%ten%' order by lastname;
lastname
-------------
Christensen
Wooten
...
```

以下示例查找第三和第四个字符为 “ea” 的城市 。 : 

```
select distinct city from users where city like '__EA%' order by city;
city
-------------
Brea
Clearwater
Great Falls
Ocean City
Olean
Wheaton
(6 rows)
```

以下示例使用默认转义字符串（\$1\$1）搜索包含“start\$1”（文本 `start` 后跟下划线 `_`)的字符串：

```
select tablename, "column" from my_table_def 


where "column" like '%start\\_%'
limit 5;

     tablename     |    column
-------------------+---------------
 my_s3client      | start_time
 my_tr_conflict   | xact_start_ts
 my_undone        | undo_start_ts
 my_unload_log    | start_time
 my_vacuum_detail | start_row
(5 rows)
```

以下示例指定“^”作为转义字符，然后使用该转义字符搜索包含“start\$1”（文本 `start` 后跟下划线 `_`)的字符串：

```
select tablename, "column" from my_table_def 

where "column" like '%start^_%' escape '^' 
limit 5;

     tablename     |    column
-------------------+---------------
 my_s3client      | start_time
 my_tr_conflict   | xact_start_ts
 my_undone        | undo_start_ts
 my_unload_log    | start_time
 my_vacuum_detail | start_row
(5 rows)
```