MySQL提供標準的SQL模式匹配以及基於擴展的正則表達式的模式匹配形式,相似於Unix實用程序(如vi
、grep
和sed
)使用的正則表達式。mysql
SQL模式匹配使你能夠使用_
來匹配任何單個字符,使用%
來匹配任意數量的字符(包括零個字符),在MySQL中,SQL模式默認狀況下不區分大小寫。這裏顯示了一些例子,使用SQL模式時不要使用=
或<>
,請改用LIKE
或NOT LIKE
比較運算符。正則表達式
要查找以b
開頭的名稱:sql
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 | +--------+--------+---------+------+------------+------------+
要查找以fy
結尾的名稱:segmentfault
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 | +--------+--------+---------+------+------------+-------+
要查找包含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 | +----------+-------+---------+------+------------+------------+
MySQL提供的另外一種模式匹配使用擴展的正則表達式,當你測試此類模式的匹配項時,請使用REGEXP_LIKE()
函數(或REGEXP
或RLIKE
運算符,它們是REGEXP_LIKE()
的同義詞)。測試
如下列表描述了擴展的正則表達式的一些特徵:code
.
匹配任何單個字符。[...]
匹配括號內的任何字符,例如,[abc]
匹配a
、b
或c
,要命名一系列字符,請使用短劃線,[a-z]
匹配任何字母,而[0-9]
匹配任何數字。*
匹配前一項的零個或多個實例,例如,x*
匹配任意數量的x
字符,[0-9]*
匹配任意數量的數字,而.*
匹配任意數量的任何東西。LIKE
模式匹配不一樣,LIKE
模式匹配僅在模式匹配整個值時纔會成功。^
或在模式的結束處使用$
。爲了演示擴展的正則表達式的工做原理,先前顯示的LIKE
查詢將在此處重寫以使用REGEXP_LIKE()
。排序
要查找以b
開頭的名稱,請使用^
來匹配名稱的開頭:ci
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 | +--------+--------+---------+------+------------+------------+
要強制將正則表達式比較區分大小寫,請使用區分大小寫的排序規則,或使用BINARY
關鍵字使其中一個字符串成爲二進制字符串,或指定c
匹配控制字符,這些查詢中的每個僅匹配名稱開頭的小寫字母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');
要查找以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 | +--------+--------+---------+------+------------+-------+
要查找包含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 | +----------+-------+---------+------+------------+------------+
由於正則表達式模式匹配,若是它出如今值的任何位置,則在上一個查詢中沒必要在模式的任何一側放置通配符以使其與整個值匹配,就像SQL模式同樣。
要查找包含五個字符的名稱,請使用^
和$
來匹配名稱的開頭和結尾,以及中間五個.
實例:
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 | +-------+--------+---------+------+------------+-------+
你還能夠使用{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 | +-------+--------+---------+------+------------+-------+