教程集 www.jiaochengji.com
教程集 >  数据库  >  mysql  >  正文 mysql模糊查询语句用法举例

mysql模糊查询语句用法举例

发布时间:2016-04-30   编辑:jiaochengji.com
mysql提供标准的sql模式匹配,以及一种基于象unix实用程序如vi、grep和sed的扩展正则表达式,分享下mysql模糊查询的相关内容。

mysql中模糊查询的四种用法:

1,%:表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示。
比如 select * from [user] where u_name like '%三%'
将会把u_name为“张三”,“张猫三”、“三脚猫”,“唐三藏”等等有“三”的记录全找出来。
另外,如果需要找出u_name中既有“三”又有“猫”的记录,请使用and条件
select * from [user] where u_name like '%三%' and u_name like '%猫%'
若使用 select * from [user] where u_name like '%三%猫%'

虽然能搜索出“三脚猫”,但不能搜索出符合条件的“张猫三”。

2,_: 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句:
比如 select * from [user] where u_name like '_三_'
只找出“唐三藏”这样u_name为三个字且中间一个字是“三”的;

再比如 select * from [user] where u_name like '三__'; 只找出“三脚猫”这样name为三个字且第一个字是“三”的;

3,[ ]:表示括号内所列字符中的一个(类似正则表达式)。指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个。
比如 select * from [user] where u_name like '[张李王]三' 将找出“张三”、“李三”、“王三”(而不是“张李王三”);
如 [ ] 内有一系列字符(01234、abcde之类的)则可略写为“0-4”、“a-e”
select * from [user] where u_name like '老[1-9]' 将找出“老1”、“老2”、……、“老9”;

4,[^ ] :表示不在括号所列之内的单个字符。其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符。
比如 select * from [user] where u_name like '[^张李王]三' 将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等;
select * from [user] where u_name like '老[^1-4]'; 将排除“老1”到“老4”,寻找“老5”、“老6”、……

5,查询内容包含通配符时
由于通配符的缘故,导致我们查询特殊字符“%”、“_”、“[”的语句无法正常实现,而把特殊字符用“[ ]”括起便可正常查询。据此我们写出以下函数:
function sqlencode(str) str=replace(str,"';","';';")
str=replace(str,"[","[[]") ';此句一定要在最先 str=replace(str,"_","[_]") str=replace(str,"%","[%]") sqlencode=str end function

附,mysql模糊查询语法

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].";

您可能感兴趣的文章:
mysql语句带参数模糊查询匹配问题
sql模糊查询实例详解
mysql 子查询与join性能对比
mysql子查询的五种方式
mysql模糊查询语句用法举例
mysql打开慢查询日志的方法
mysql优化之如何定位效率较低的SQL
mysql开启慢查询以检查查询慢的语句
有关sql模糊查询的介绍
有关mysql单表多关键字模糊查询的方法介绍

关键词: mysql  模糊查询   
[关闭]
~ ~