diff --git a/docs/sql-manual/basic-element/operators/conditional-operators/pattern-matching-operators.md b/docs/sql-manual/basic-element/operators/conditional-operators/pattern-matching-operators.md index 1e0571c6044f6..0b76005fc9fc4 100644 --- a/docs/sql-manual/basic-element/operators/conditional-operators/pattern-matching-operators.md +++ b/docs/sql-manual/basic-element/operators/conditional-operators/pattern-matching-operators.md @@ -42,13 +42,14 @@ The LIKE condition specifies a test involving pattern matching. The equality com The syntax is as follows: ```sql - [ NOT ] LIKE + [ NOT ] LIKE [ ESCAPE 'char_escape' ] ``` Where: - `char1` is a character expression (such as a character column), known as the search value. - `char2` is a character expression, usually a string literal, known as the pattern. +- `char_escape` (optional) is a character expression and must be a character of length 1 (under ascii encoding). It allows you to define escape characters, and if you do not provide char_escape, the default ' \ ' is an escape character. Both character expressions (`char1`, `char2`) can be any of CHAR, VARCHAR, or STRING data types. If they are different, Doris will convert them all to VARCHAR or STRING. @@ -57,6 +58,39 @@ Patterns can include special pattern matching characters: - The underscore (`_`) in the pattern matches exactly one character in the value. - The percent sign (`%`) in the pattern can match zero or multiple characters in the value. The pattern `%` cannot match NULL. +### Example + + +```sql +select "%a" like "\%_"; +``` + +The result is as follows, because "%" is a special character, it needs to be escaped with "\%" to match correctly. + +```text ++-----------------+ +| "%a" like "\%_" | ++-----------------+ +| 1 | ++-----------------+ +``` + + +```sql +select "%a" like "a%_" ESCAPE "a"; +``` + +The difference from the previous example is that "a" is specified as the escape character. + +```text ++----------------------------+ +| "%a" like "a%_" ESCAPE "a" | ++----------------------------+ +| 1 | ++----------------------------+ +``` + + ### REGEXP (RLIKE) REGEXP is similar to the LIKE condition, differing in that REGEXP performs regular expression matching, rather than the simple pattern matching performed by LIKE. This condition uses a set of input characters defined by a character to evaluate strings. diff --git a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/operators/conditional-operators/pattern-matching-operators.md b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/operators/conditional-operators/pattern-matching-operators.md index b4dcd2a535b7c..042cfb42c5a96 100644 --- a/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/operators/conditional-operators/pattern-matching-operators.md +++ b/i18n/zh-CN/docusaurus-plugin-content-docs/current/sql-manual/basic-element/operators/conditional-operators/pattern-matching-operators.md @@ -42,13 +42,14 @@ LIKE 条件指定一个涉及模式匹配的测试。等值比较运算符(= 语法如下: ```sql - [ NOT ] LIKE + [ NOT ] LIKE [ ESCAPE 'char_escape' ] ``` 其中: -- char1 是一个字符表达式(如字符列),称为搜索值。 -- char2 是一个字符表达式,通常是一个字面量,称为模式。 +- char1 是一个字符表达式(如字符列),称为搜索串。 +- char2 是一个字符表达式,通常是一个字面量,称为模式串。 +- char_escape (可选) 是一个字符表达式,必须是一个长度为1的字符 ( ascii 编码下 )。 它允许您定义转义字符,如果您不提供char_escape,则默认 ‘ \ ’是转义字符。 所有字符表达式(char1、char2)都可以是 CHAR、VARCHAR 或 STRING 数据类型中的任何一种。如果它们不同,则 Doris 会将它们全部转换为 VARCHAR 或者 STRING。 @@ -57,6 +58,38 @@ LIKE 条件指定一个涉及模式匹配的测试。等值比较运算符(= - 模式中的下划线 (_) 与值中的一个字符完全匹配。 - 模式中的百分号 (%) 可以与值中的零个或多个字符匹配。模式 '%' 不能与 NULL 匹配。 +### 示例 + +```sql +select "%a" like "\%_"; +``` + +结果如下,因为 "%" 是特殊字符,所以需要用 "\%" 进行转义才能正确匹配。 + +```text ++-----------------+ +| "%a" like "\%_" | ++-----------------+ +| 1 | ++-----------------+ +``` + + +```sql +select "%a" like "a%_" ESCAPE "a"; +``` + +与之前的例子的区别在于有指定 "a" 作为转义字符。 + +```text ++----------------------------+ +| "%a" like "a%_" ESCAPE "a" | ++----------------------------+ +| 1 | ++----------------------------+ +``` + + ### REGEXP(RLIKE) REGEXP 与 LIKE 条件类似,不同之处在于 REGEXP 执行正则表达式匹配,而不是 LIKE 执行的简单模式匹配。此条件使用输入字符集定义的字符来评估字符串。