

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 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>

 *運算式*   
有效的 UTF-8 字元表達式，例如資料欄的名稱。

LIKE  
LIKE 會進行區分大小寫的模式比對。若要對多位元組字元執行不區分大小寫的模式比對，請在具有 LIKE 條件的 *expression* 和 *pattern* 上使用 [LOWER](LOWER.md) 函數。  
相較於比較述詞，例如 = 和 <>，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_tw/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_tw/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)，來搜尋包含「\$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)
```

下列的範例將「^」指定為逸出字元，然後使用該逸出字元來搜尋包含「\$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)
```