mysql的匹配模式

來源:互聯網
上載者:User
    MySQL提供標準的SQL模式比對,以及一種基於象Unix公用程式如vi、grep和sed的擴充Regex模式比對的格式。     SQL的模式比對允許你使用“_”匹配任何單個字元,而“%”匹配任意數目字元(包括零個字元)。在 MySQL中,SQL的模式預設是忽略大小寫。    下面顯示一些例子。注意在你使用SQL模式時,你不能使用=或!=;而使用LIKE或NOT LIKE比較操作符。為了找出以“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 |+--------+--------+---------+------+------------+------------+為了找出以“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 |+--------+--------+---------+------+------------+-------+為了找出包含一個“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 |+----------+-------+---------+------+------------+------------+為了找出包含正好5個字元的名字,使用“_”模式字元: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 |+-------+--------+---------+------+------------+-------+    由MySQL提供的模式比對的其他類型是使用擴充Regex。當你對這類模式進行匹配測試時,使用REGEXP和NOT REGEXP操作符(或RLIKE和NOT RLIKE,它們是同義字)。擴充Regex的一些字元是:“.” 匹配任何單個的字元。 一個字元類“[...]”匹配在方括弧內的任何字元。例如,“[abc]”匹配“a”、“b”或“c”。為了命名字元的一個範圍,使用一個“-”。“[a-z]”匹配任何小寫字母,而“[0-9]”匹配任何數字。 “ * ”匹配零個或多個在它前面的東西。例如,“x*”匹配任何數量的“x”字元,“[0-9]*”匹配的任何數量的數字,而“.*”匹配任何數量的任何東西。 Regex是區分大小寫,但是如果你希望,你能使用一個字元類匹配兩種寫法。例如,“[aA]”匹配小寫或大寫的“a”而“[a-zA-Z]”匹配兩種寫法的任何字母。 如果它出現在被測試值的任何地方,模式就匹配(只要他們匹配整個值,SQL模式比對)。 為了定位一個模式以便它必須匹配被測試值的開始或結尾,在模式開始處使用“^”或在模式的結尾用“$”。 為了說明擴充Regex如何工作,上面所示的LIKE查詢在下面使用REGEXP重寫:為了找出以“b”開頭的名字,使用“^”匹配名字的開始並且“[bB]”匹配小寫或大寫的“b”:mysql> SELECT * FROM pet WHERE name REGEXP "^[bB]";+--------+--------+---------+------+------------+------------+| 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”結尾的名字,使用“$”匹配名字的結尾:mysql> SELECT * FROM pet WHERE name REGEXP "fy$";+--------+--------+---------+------+------------+-------+| name | owner | species | sex | birth | death |+--------+--------+---------+------+------------+-------+| Fluffy | Harold | cat | f | 1993-02-04 | NULL || Buffy | Harold | dog | f | 1989-05-13 | NULL |+--------+--------+---------+------+------------+-------+為了找出包含一個“w”的名字,使用“[wW]”匹配小寫或大寫的“w”:mysql> SELECT * FROM pet WHERE name REGEXP "[wW]";+----------+-------+---------+------+------------+------------+| 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模式那樣。為了找出包含正好5個字元的名字,使用“^”和“$”匹配名字的開始和結尾,和5個“.”執行個體在兩者之間:mysql> SELECT * FROM pet WHERE name REGEXP "^.....$";+-------+--------+---------+------+------------+-------+| 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 name REGEXP "^.{5}$";+-------+--------+---------+------+------------+-------+| name | owner | species | sex | birth | death |+-------+--------+---------+------+------------+-------+| Claws | Gwen | cat | m | 1994-03-17 | NULL || Buffy | Harold | dog | f | 1989-05-13 | NULL |+-------+--------+---------+------+------------+-------+
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.