MySQL provides standard SQL pattern matching as well as a form of pattern matching based on extended regular expressions similar to those used by Unix utilities such as vi, grep, and sed.MySQL提供了标准的SQL模式匹配,以及一种基于扩展正则表达式的模式匹配形式,类似于Unix实用程序(如vi、grep和sed)所使用的正则表达式。
SQL pattern matching enables you to use SQL模式匹配允许您使用_
to match any single character and %
to match an arbitrary number of characters (including zero characters). _
匹配任何单个字符,使用%
匹配任意数量的字符(包括零个字符)。In MySQL, SQL patterns are case-insensitive by default. 在MySQL中,默认情况下SQL模式不区分大小写。Some examples are shown here. 这里显示了一些示例。Do not use 当您使用SQL模式时,不要使用=
or <>
when you use SQL patterns. =
或<>
。Use the 请改用LIKE
or NOT LIKE
comparison operators instead.LIKE
或NOT LIKE
比较运算符。
To find names beginning with 要查找以b
:b
开头的名称,请执行以下操作:
mysql> SELECT * FROM pet WHERE name LIKE 'b%';
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
To find names ending with 要查找以fy
:fy
结尾的名称,请执行以下操作:
mysql> SELECT * FROM pet WHERE name LIKE '%fy';
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
To find names containing a 要查找包含w
:w
的名称,请执行以下操作:
mysql> SELECT * FROM pet WHERE name LIKE '%w%';
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
+----------+-------+---------+------+------------+------------+
To find names containing exactly five characters, use five instances of the 要查找正好包含五个字符的名称,请使用_
pattern character:_
模式字符的五个实例:
mysql> SELECT * FROM pet WHERE name LIKE '_____';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
The other type of pattern matching provided by MySQL uses extended regular expressions. MySQL提供的另一种模式匹配使用扩展正则表达式。When you test for a match for this type of pattern, use the 测试此类型模式的匹配时,请使用REGEXP_LIKE()
function (or the REGEXP
or RLIKE
operators, which are synonyms for REGEXP_LIKE()
).REGEXP_LIKE()
函数(或REGEXP
或RLIKE
运算符,它们是REGEXP_LIKE()
的同义词)。
The following list describes some characteristics of extended regular expressions:下表介绍了扩展正则表达式的一些特性:
.
matches any single character..
匹配任何单个字符。
A character class 字符类[...]
matches any character within the brackets. [...]
匹配括号内的任何字符。For example, 例如,[abc]
matches a
, b
, or c
. [abc]
匹配a
、b
或c
。To name a range of characters, use a dash. 要命名一系列字符,请使用破折号。[a-z]
matches any letter, whereas [0-9]
matches any digit.[a-z]
匹配任何字母,而[0-9]
匹配任何数字。
*
matches zero or more instances of the thing preceding it. *
匹配其前面事物的零个或多个实例。For example, 例如,x*
matches any number of x
characters, [0-9]*
matches any number of digits, and .*
matches any number of anything.x*
匹配任意数目的x
字符,[0-9]*
匹配任意数目的数字,.*
匹配任意数量的任何字符。
A regular expression pattern match succeeds if the pattern matches anywhere in the value being tested. 如果模式匹配被测试值中的任何位置,则正则表达式模式匹配成功。(This differs from a (这与LIKE
pattern match, which succeeds only if the pattern matches the entire value.)LIKE
模式匹配不同,后者仅在模式匹配整个值时才成功。)
To anchor a pattern so that it must match the beginning or end of the value being tested, use 若要锚定模式,使其必须与测试值的开头或结尾匹配,请在模式的开头使用^
at the beginning or $
at the end of the pattern.^
,在模式末尾使用$
。
To demonstrate how extended regular expressions work, the 为了演示扩展正则表达式是如何工作的,这里重写了前面显示的LIKE
queries shown previously are rewritten here to use REGEXP_LIKE()
.LIKE
查询,以使用REGEXP_LIKE()
。
To find names beginning with 要查找以b
, use ^
to match the beginning of the name:b
开头的名称,请使用^
匹配名称的开头:
mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, '^b');
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1979-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
To force a regular expression comparison to be case-sensitive, use a case-sensitive collation, or use the 要强制正则表达式比较区分大小写,请使用区分大小写的排序规则,或使用BINARY
keyword to make one of the strings a binary string, or specify the c
match-control character. BINARY
关键字将其中一个字符串设置为二进制字符串,或指定c
匹配控制字符。Each of these queries matches only lowercase 每个查询只匹配名称开头的小写b
at the beginning of a name:b
:
SELECT * FROM pet WHERE REGEXP_LIKE(name, '^b' COLLATE utf8mb4_0900_as_cs); SELECT * FROM pet WHERE REGEXP_LIKE(name, BINARY '^b'); SELECT * FROM pet WHERE REGEXP_LIKE(name, '^b', 'c');
To find names ending with 要查找以fy
, use $
to match the end of the name:fy
结尾的名称,请使用$
匹配名称的结尾:
mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, 'fy$');
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
To find names containing a 要查找包含w
, use this query:w
的名称,请使用以下查询:
mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, 'w');
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
+----------+-------+---------+------+------------+------------+
Because a regular expression pattern matches if it occurs anywhere in the value, it is not necessary in the previous query to put a wildcard on either side of the pattern to get it to match the entire value as would be true with an SQL pattern.因为正则表达式模式如果出现在值中的任何位置就会匹配,所以在前面的查询中不必在模式的任一侧放置通配符,以使其与整个值匹配,就像SQL模式一样。
To find names containing exactly five characters, use 若要查找正好包含五个字符的名称,请使用^
and $
to match the beginning and end of the name, and five instances of .
in between:^
和$
匹配名称的开头和结尾以及其间的五个.
实例:
mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, '^.....$');
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
You could also write the previous query using the 您还可以使用{
(“repeat-n
}n
-times”) operator:{
(重复n
}n
次)操作符编写前面的查询:
mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, '^.{5}$');
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
For more information about the syntax for regular expressions, see Section 12.8.2, “Regular Expressions”.有关正则表达式语法的更多信息,请参阅第12.8.2节,“正则表达式”。