bitscn.com
mysql提供标准的sql模式匹配,以及一种基于象unix实用程序如vi、grep和sed的扩展正则表达式
模式匹配的格式。
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提供的模式匹配的其他类型是使用扩展正则表达式。当你对这类模式进行匹配测试时,使用
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 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 |
+-------+--------+---------+------+------------+-------+
查找数字和其他的模糊查询语句
select * from pet where name regexp [^a-za-z].;
bitscn.com