Home >Database >Mysql Tutorial >MySQL 通配符学习小结

MySQL 通配符学习小结

WBOY
WBOYOriginal
2016-05-31 08:47:211073browse

MySQL 通配符

SQL的模式匹配同意你使用“_”匹配不论什么单个字符,而“%”匹配随意数目字符(包含零个字符)。在 MySQL中,SQL的模式缺省是忽略大写和小写的。以下显示一些样例。

注意在你使用SQL模式时,你不能使用=或!=;而使用LIKE或NOT LIKE比較操作符。

为了找出以“b”开头的名字:

mysql> SELECT * FROM pet WHEREnameLIKE "

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 WHEREnameLIKE"%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 WHEREnameLIKE"%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 WHEREnameLIKE "_____";

+-------+--------+---------+------+------------+-------+

| name | owner | species | sex | birth | death |

+-------+--------+---------+------+------------+-------+

|

Claws

| Gwen | cat | m | 1994-03-17 | NULL |

|

Buffy

| Harold | dog | f | 1989-05-13 | NULL |

+-------+--------+---------+------+------------+-------+

由MySQL提供的模式匹配的其它类型是使用扩展正則表達式。当你对这类模式进行匹配測试时,使用REGEXP和NOT REGEXP操作符(或RLIKE和NOT RLIKE,它们是同义词)。

扩展正則表達式的一些字符是:

“.”匹配不论什么单个的字符。

一个字符类“[...]”匹配在方括号内的不论什么字符。比如,“[abc]”匹配“a”、“b”或“c”。为了命名字符的一个范围,使用一个“-”。“[a-z

]

” 匹配不论什么小写字母,而“[0-9]”匹配不论什么数字。

*

”匹配零个或多个在它前面的东西。比如,“x*”匹配不论什么数量的“x”字符,“[0-9]*”匹配的不论什么数量的数字,而“.*”匹配不论什么数量的不论什么东西。

正則表達式是区分大写和小写的,可是假设你希望,你能使用一个字符类匹配两种写法。比如,“[aA]”匹配小写或大写的“a”而“[a-zA-Z]”匹配两种写法的不论什么字母。

假设它出如今被測试值的不论什么地方,模式就匹配(仅仅要他们匹配整个值,SQL模式匹配)。

为了定位一个模式以便它必须匹配被測试值的開始或结尾,在模式開始处使用“^”或在模式的结尾用“$”。

为了说明扩展正則表達式怎样工作,上面所看到的的LIKE查询在以下使用REGEXP重写:

为了找出以“b”开头的名字,使用“^”匹配名字的開始而且“[bB]”匹配小写或大写的“b”:

mysql> SELECT * FROM pet WHEREnameREGEXP "^[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 WHEREnameREGEXP "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 WHEREnameREGEXP "[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 WHEREnameREGEXP "^.....$";

+-------+--------+---------+------+------------+-------+

| 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 WHEREnameREGEXP "^.{5}$";

+-------+--------+---------+------+------------+-------+

| name | owner | species | sex | birth | death |

+-------+--------+---------+------+------------+-------+

|

Claws

| Gwen | cat | m | 1994-03-17 | NULL |

|

Buffy

| Harold | dog | f | 1989-05-13 | NULL |

+-------+--------+---------+------+------------+-------+

http://www.playhosts.com/bbs/read.php?tid=12357

=====================================================================

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn