《MysqL入门MysqL模糊查询语句用法举例》要点:
本文介绍了MysqL入门MysqL模糊查询语句用法举例,希望对您有用。如果有疑问,可以联系我们。
导读:
MysqL中模糊
查询的四种
用法:1,%:表现任意0个或多个字符.可匹配任意类型和长度的字符,有些情况下若是
中文,请使用两个百分号(%%)表现...
MysqL中模糊查询的四种用法:MysqL学习
1,%:表示任意0个或多个字符.可匹配任意类型和长度的字符,请使用两个百分号(%%)表示. @H_404_13@好比 select * from [user] where u_name like '%三%' @H_404_13@将会把u_name为“张三”,“张猫三”、“三脚猫”,“唐三藏”等等有“三”的记录全找出来. @H_404_13@另外,如果需要找出u_name中既有“三”又有“猫”的记录,请使用and条件 @H_404_13@select * from [user] where u_name like '%三%' and u_name like '%猫%' @H_404_13@若使用 select * from [user] where u_name like '%三%猫%'MysqL学习
虽然能搜索出“三脚猫”,但不克不及搜索出符合条件的“张猫三”.MysqL学习
2,_: 表示任意单个字符.匹配单个任意字符,它常用来限制表达式的字符长度语句: @H_404_13@好比 select * from [user] where u_name like '_三_' @H_404_13@只找出“唐三藏”这样u_name为三个字且中间一个字是“三”的;MysqL学习
再好比 select * from [user] where u_name like '三__'; 只找出“三脚猫”这样name为三个字且第一个字是“三”的;MysqL学习
3,[ ]:表示括号内所列字符中的一个(类似正则表达式).指定一个字符、字符串或范围,要求所匹配对象为它们中的任一个. @H_404_13@好比 select * from [user] where u_name like '[张李王]三' 将找出“张三”、“李三”、“王三”(而不是“张李王三”); @H_404_13@如 [ ] 内有一系列字符(01234、abcde之类的)则可略写为“0-4”、“a-e” @H_404_13@select * from [user] where u_name like '老[1-9]' 将找出“老1”、“老2”、……、“老9”;MysqL学习
4,[^ ] :表示不在括号所列之内的单个字符.其取值和 [] 相同,但它要求所匹配对象为指定字符以外的任一个字符. @H_404_13@好比 select * from [user] where u_name like '[^张李王]三' 将找出不姓“张”、“李”、“王”的“赵三”、“孙三”等; @H_404_13@select * from [user] where u_name like '老[^1-4]'; 将排除“老1”到“老4”,寻找“老5”、“老6”、……MysqL学习
5,查询内容包括通配符时 @H_404_13@由于通配符的缘故,导致我们查询特殊字符“%”、“_”、“[”的语句无法正常实现,而把特殊字符用“[ ]”括起便可正常查询.据此我们写出以下函数: @H_404_13@function sqlencode(str) str=replace(str,"';","';';") @H_404_13@str=replace(str,"[","[[]") ';此句一定要在最先 str=replace(str,"_","[_]") str=replace(str,"%","[%]") sqlencode=str end functionMysqL学习
附,MysqL隐约查询语法MysqL学习
MysqL提供尺度的sql模式匹配,以及一种基于象unix实用程序如vi、grep和sed的扩展正则表达式MysqL学习
模式匹配的格局.MysqL学习
sql的模式匹配允许你使用“_”匹配任何单个字符,而“%”匹配任意数目字符(包含零个字符).MysqL学习
在 MysqL中,sql的模式缺省是忽略大小写的.下面显示一些例子.注意在你使用sql模式时,你不能使用=或!=;而使用like或not like比拟操作符.MysqL学习
为了找出以“b”开首的名字:@H_404_13@ MysqL学习
MysqL> select * from pet where name like "b%"; @H_
404_13@+--------+--------+---------+------+------------+------------+@H_
404_13@| name | owner | species | sex | birth | death |@H_
404_13@+--------+--------+---------+------+------------+------------+@H_
404_13@| buffy | harold | dog | f | 1989-05-13 | null |@H_
404_13@| bowser | diane | dog | m | 1989-08-31 | 1995-07-29 |@H_
404_13@+--------+--------+---------+------+------------+------------+
为了找出以“fy”结尾的名字:@H_404_13@ MysqL学习
MysqL> select * from pet where name like "%fy"; @H_
404_13@+--------+--------+---------+------+------------+-------+@H_
404_13@| name | owner | species | sex | birth | death |@H_
404_13@+--------+--------+---------+------+------------+-------+@H_
404_13@| fluffy | harold | cat | f | 1993-02-04 | null |@H_
404_13@| buffy | harold | dog | f | 1989-05-13 | null |@H_
404_13@+--------+--------+---------+------+------------+-------+
为了找出包括一个“w”的名字:@H_404_13@ MysqL学习
MysqL> select * from pet where name like "%w%"; @H_
404_13@+----------+-------+---------+------+------------+------------+@H_
404_13@| name | owner | species | sex | birth | death |@H_
404_13@+----------+-------+---------+------+------------+------------+@H_
404_13@| claws | gwen | cat | m | 1994-03-17 | null |@H_
404_13@| bowser | diane | dog | m | 1989-08-31 | 1995-07-29 |@H_
404_13@| whistler | gwen | bird | null | 1997-12-09 | null |@H_
404_13@+----------+-------+---------+------+------------+------------+
为了找出包括正好5个字符的名字,使用“_”模式字符:@H_404_13@ MysqL学习
MysqL> select * from pet where name like "_____"; @H_
404_13@+-------+--------+---------+------+------------+-------+@H_
404_13@| name | owner | species | sex | birth | death |@H_
404_13@+-------+--------+---------+------+------------+-------+@H_
404_13@| claws | gwen | cat | m | 1994-03-17 | null |@H_
404_13@| buffy | harold | dog | f | 1989-05-13 | null |@H_
404_13@+-------+--------+---------+------+------------+-------+
由MysqL提供的模式匹配的其他类型是使用扩大正则表达式.当你对这类模式进行匹配测试时,使用regexp和not regexp操作符(或rlike和not rlike,它们是同义词).MysqL学习
扩大正则表达式的一些字符是:MysqL学习
“.”匹配任何单个的字符. @H_404_13@一个字符类“[...]”匹配在方括号内的任何字符.例如,“[abc]”匹配“a”、“b”或“c”. @H_404_13@为了命名字符的一个范围,使用一个“-”.“[a-z]”匹配任何小写字母,而“[0-9]”匹配任何数字. @H_404_13@“ * ”匹配零个或多个在它前面的东西.例如,“x*”匹配任何数量的“x”字符,“[0-9]*” @H_404_13@匹配的任何数量的数字,而“.*”匹配任何数量的任何东西. @H_404_13@正则表达式是区分大小写的,但是如果你希望,你能使用一个字符类匹配两种写法.例如,“[aa]”匹配小写或大写的“a”而“[a-za-z]”匹配两种写法的任何字母. @H_404_13@如果它出现在被测试值的任何地方,模式就匹配(只要他们匹配整个值,sql模式匹配). @H_404_13@为了定位一个模式以便它必需匹配被测试值的开始或结尾,在模式开始处使用“^”或在模式的结尾用“$”. @H_404_13@为了说明扩展正则表达式如何工作,上面所示的like查询在下面使用regexp重写:MysqL学习
为了找出以“b”开头的名字,使用“^”匹配名字的开始而且“[bb]”匹配小写或大写的“b”:@H_404_13@ MysqL学习
MysqL> select * from pet where name regexp "^[bb]"; @H_
404_13@+--------+--------+---------+------+------------+------------+@H_
404_13@| name | owner | species | sex | birth | death |@H_
404_13@+--------+--------+---------+------+------------+------------+@H_
404_13@| buffy | harold | dog | f | 1989-05-13 | null |@H_
404_13@| bowser | diane | dog | m | 1989-08-31 | 1995-07-29 |@H_
404_13@+--------+--------+---------+------+------------+------------+
为了找出以“fy”结尾的名字,使用“$”匹配名字的结尾:@H_404_13@ MysqL学习
MysqL> select * from pet where name regexp "fy$"; @H_
404_13@+--------+--------+---------+------+------------+-------+@H_
404_13@| name | owner | species | sex | birth | death |@H_
404_13@+--------+--------+---------+------+------------+-------+@H_
404_13@| fluffy | harold | cat | f | 1993-02-04 | null |@H_
404_13@| buffy | harold | dog | f | 1989-05-13 | null |@H_
404_13@+--------+--------+---------+------+------------+-------+
为了找出包括一个“w”的名字,使用“[ww]”匹配小写或大写的“w”:@H_404_13@ MysqL学习
MysqL> select * from pet where name regexp "[ww]"; @H_
404_13@+----------+-------+---------+------+------------+------------+@H_
404_13@| name | owner | species | sex | birth | death |@H_
404_13@+----------+-------+---------+------+------------+------------+@H_
404_13@| claws | gwen | cat | m | 1994-03-17 | null |@H_
404_13@| bowser | diane | dog | m | 1989-08-31 | 1995-07-29 |@H_
404_13@| whistler | gwen | bird | null | 1997-12-09 | null |@H_
404_13@+----------+-------+---------+------+------------+------------+
既然如果一个正规表达式呈现在值的任何地方,其模式匹配了,就不必再先前的查询中在模式的两方面放置一个通配符以使得它匹配整个值,就像如果你使用了一个sql模式那样.MysqL学习
为了找出包括正好5个字符的名字,使用“^”和“$”匹配名字的开始和结尾,和5个“.”实例在两者之间:@H_404_13@ MysqL学习
MysqL> select * from pet where name regexp "^.....$"; @H_
404_13@+-------+--------+---------+------+------------+-------+@H_
404_13@| name | owner | species | sex | birth | death |@H_
404_13@+-------+--------+---------+------+------------+-------+@H_
404_13@| claws | gwen | cat | m | 1994-03-17 | null |@H_
404_13@| buffy | harold | dog | f | 1989-05-13 | null |@H_
404_13@+-------+--------+---------+------+------------+-------+
也可以使用“{n}”“反复n次”操作符重写先前的查询:@H_404_13@ MysqL学习
MysqL> select * from pet where name regexp "^.{5}$"; @H_
404_13@+-------+--------+---------+------+------------+-------+@H_
404_13@| name | owner | species | sex | birth | death |@H_
404_13@+-------+--------+---------+------+------------+-------+@H_
404_13@| claws | gwen | cat | m | 1994-03-17 | null |@H_
404_13@| buffy | harold | dog | f | 1989-05-13 | null |@H_
404_13@+-------+--------+---------+------+------------+-------+
查找数字和其他的隐约查询语句@H_404_13@select * from pet where name regexp "[^a-za-z].";MysqL学习
欢迎参与《MysqL入门MysqL模糊查询语句用法举例》讨论,分享您的想法,编程之家PHP学院为您提供专业教程。