当前位置: 移动技术网 > IT编程>数据库>Mysql > 老生常谈MYSQL模式匹配 REGEXP和like的用法

老生常谈MYSQL模式匹配 REGEXP和like的用法

2017年12月12日  | 移动技术网IT编程  | 我要评论

like

like要求整个数据都要匹配,而regexp只需要部分匹配即可。 
也就是说,用like,必须这个字段的所有内容满足条件,而regexp只需要有任何一个片段满足即可。

mysql提供标准的sql模式匹配(like),以及一种基于象unix实用程序如vi、grep和sed的扩展正则表达式模式匹配的格式(regexp)。
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 |
+-------+--------+---------+------+------------+-------+

regexp

另外一种匹配是基于正则表达式的。当你对这类模式进行匹配测试时,使用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 |
+----------+-------+---------+------+------------+------------+

[^……],匹配不包含在[]的字符,如查询出除了w/z/s开头之外的人名

select name from 表名 where name regexp '^[^wzs]';

*,重复0次或多次,熟悉javascript正则的同学都知道 

'str*'可以匹配st/str/strr/strrr……

?,重复0次或1次

'str?'可以匹配st/str

+,重复1次或多次

'str+'可以匹配str/strr/strrr/strrrr……

相比javascript里面的正则而言,这里的正则是简化版的,没有惰性匹配/贪婪匹配,[]内不支持\w\s\d这种语法,也不支持中文,相对简单

以上这篇老生常谈mysql模式匹配 regexp和like的用法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网