模糊查询

模糊查询是针对字符串操作的,类似正则表达式,没有正则表达式强大

通配符:_ 、% 、[] 、

_ 表示任意的单个字符

--查询MyStudent表中任意姓张,名字两个字的姓名
select * from MyStudent where fname like '张_'
--查询MyStudent表中任意姓张,名字三个字的姓名
select * from MyStudent where fname like '张__'

%表示匹配任意多个任意字符

--查询MyStudent表中任意姓张的(第一个为张)的姓名字数不限的姓名
select * from MyStudent where fname like '张%'
--查询MyStudent表中姓张的名字两个字的
select * from MyStudent where fname like '张%'and len(fname) = 2

[]表示筛选范

--查询表中所有姓张第二个为数字第三个为妹的名字
select * from MyStudent where fname like '张[0-9]妹'

select * from MyStudent where fname like '张[a-z]妹'

select * from MyStudent where fname like '张[0-9a-z]妹'--中间为1个字母和和1个数字,字母大小写可以通过约束设定,不区分大小写

select * from MyStudent where fname like '张[!0-9]妹' --中间不为数字

select * from MyStudent where fname not like '张[0-9]妹'--不能以张开头妹结尾中间不能是数字同上不一样
--查询出表中包含通配符%的姓名 

 select * from MyStudent where fname  like '%[%]%'--通配符放到[]中就转义了不是通配符了

 
--查询表中包含[的姓名
 select * from MyStudent where fname  like '%/[%' escape '/' --指定通配符为/

--查询表中包含通配符[]的姓名
 select * from MyStudent where fname  like '%/[/]%' escape '/' --指定通配符为/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章