SQL实现模糊查询的几种方法总结

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

一、一般模糊查询

1. 单条件查询
//查询所有姓名包含“张”的记录
select * from student where name like '张'
2. 多条件查询
//查询所有姓名包含“张”,地址包含四川的记录
select * from student where name like '张' and address like '四川'

//查询所有姓名包含“张”,或者地址包含四川的记录
select * from student where name like '张' or address like '四川'

二、利用通配符查询

通配符:_ 、% 、[ ]

1. _ 表示任意的单个字符
//查询所有名字姓张,字长两个字的记录
select * from student where name like '张_'

//查询所有名字姓张,字长三个字的记录
select * from student where name like '张__'
2. % 表示匹配任意多个任意字符
//查询所有名字姓张,字长不限的记录
select * from student where name like '张%'

//查询所有名字姓张,字长两个字的记录
select * from student where name like '张%'and len(name) = 2
3. [ ]表示筛选范围
//查询所有名字姓张,第二个为数字,第三个为燕的记录
select * from student where name like '张[0-9]燕'

//查询所有名字姓张,第二个为字母,第三个为燕的记录
select * from student where name like '张[a-z]燕'

//查询所有名字姓张,中间为1个字母或1个数字,第三个为燕的名字。字母大小写可以通过约束设定,不区分大小写
select * from student where name like '张[0-9a-z]燕'

//查询所有名字姓张,第二个不为数字,第三个为燕的记录
select * from student where name like '张[!0-9]燕' 

//查询名字除了张开头妹结尾中间是数字的记录
select * from student where name not like '张[0-9]燕'
4. 查询包含通配符的字符串
//查询姓名包含通配符%的记录
 select * from student where name like '%[%]%'				//通过[]转义
 
//查询姓名包含[的记录
 select * from student where name like '%/[%' escape '/'	//通过指定'/'转义
 
//查询姓名包含通配符[]的记录
 select * from student where name like '%/[/]%' escape '/'	//通过指定'/'转义
 
  • 23
    点赞
  • 140
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Whitemeen太白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值