MySQL中的字符串情势匹配[MySQL防范]
本文“MySQL中的字符串情势匹配[MySQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
MySQL供应尺度的SQL情势匹配,以及一种基于象Unix实用程序如vi、grep和sed的扩大正则表达式情势匹配的格局.
尺度的SQL情势匹配
SQL的情势匹配答应你利用“_”匹配任何单个字符,而“%”匹配肆意数目字符(包含零个字符).在 MySQL中,SQL的情势缺省是忽视大小写的.下面显示一些例子.注意在你利用SQL情势时,你不能利用=或!=;而利用LIKE或NOT LIKE对比操作符.
比方,在表pet中,为了找出以“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供应尺度的SQL情势匹配,以及一种基于象Unix实用程序如vi、grep和sed的扩大正则表达式情势匹配的格局.
扩大正则表达式情势匹配
由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___FCKpd___5quot;; |
+--------+--------+---------+------+------------+-------+
| 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 "^.....___FCKpd___7quot;; |
+-------+--------+---------+------+------------+-------+
| 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}___FCKpd___8quot;; |
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
总结
本介绍了有关字符串情势匹配的有关知识.尺度的SQL情势匹配是SQL语言的尺度,可以被别的关系数据库系统承受.扩大正规表达式情势匹配
是按照Unix系统的尺度开辟了,普通只可以利用在MySQL上,但是其功效要比尺度的SQL情势匹配更强.
以上是“MySQL中的字符串情势匹配[MySQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |