SQL实现模糊查询的四种方法小结!

  • A+
所属分类:SQL技巧

SQL实现模糊查询的四种方法小结!

在SQL中,模糊查询是一种通过匹配字符串中的一部分或关键字来查询数据的方法,本文主要介绍了SQL实现模糊查询的四种方法小结,具有一定的参考价值,感兴趣的可以了解一下

一、一般模糊查询

1. 单条件查询

1
2
//查询所有姓名包含“张”的记录
select * from student where name like '张'

2. 多条件查询

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

二、利用通配符查询

通配符:_ 、% 、[ ]

1. _ 表示任意的单个字符

1
2
3
4
//查询所有名字姓张,字长两个字的记录
select * from student where name like '张_'
//查询所有名字姓张,字长三个字的记录
select * from student where name like '张__'

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

1
2
3
4
//查询所有名字姓张,字长不限的记录
select * from student where name like '张%'
//查询所有名字姓张,字长两个字的记录
select * from student where name like '张%'and len(name) = 2

3. [ ]表示筛选范围

1
2
3
4
5
6
7
8
9
10
//查询所有名字姓张,第二个为数字,第三个为燕的记录
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. 查询包含通配符的字符串

1
2
3
4
5
6
//查询姓名包含通配符%的记录
select * from student where name like '%[%]%'                //通过[]转义
//查询姓名包含[的记录
select * from student where name like '%/[%' escape '/'    //通过指定'/'转义
//查询姓名包含通配符[]的记录
select * from student where name like '%/[/]%' escape '/'    //通过指定'/'转义

到此这篇关于SQL实现模糊查询的四种方法小结的文章就介绍到这了。

 

学习资料见知识星球。

以上就是今天要分享的技巧,你学会了吗?若有什么问题,欢迎在下方留言。

快来试试吧,小琥 my21ke007。获取 1000个免费 Excel模板福利​​​​!

更多技巧, www.excelbook.cn

欢迎 加入 零售创新 知识星球,知识星球主要以数据分析、报告分享、数据工具讨论为主;

SQL实现模糊查询的四种方法小结!

你将获得:

1、价值上万元的专业的PPT报告模板。

2、专业案例分析和解读笔记。

3、实用的Excel、Word、PPT技巧。

4、VIP讨论群,共享资源。

5、优惠的会员商品。

6、一次付费只需99元,即可下载本站文章涉及的文件和软件。

  • 我的微信
  • weinxin
  • 我的知识星球
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: