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

目录

一、一般模糊查询

二、利用通配符查询

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

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

3. [ ]表示筛选范围

4. 查询包含通配符的字符串


一、一般模糊查询

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 '/'    //通过指定'/'转义

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


相关文章

  • Redis 管道技术——Pipeline

    Redis 流水线是一种通过一次发出多个命令而无需等待每个命令的响应来提高性能的技术,通过批处理 Redis 命令来优化往返时间。

  • HarmonyOS 鸿蒙应用开发( 六、实现自定义弹窗CustomDialog)

    自定义弹窗(CustomDialog)可用于广告、中奖、警告、软件更新等与用户交互响应操作。开发者可以通过CustomDialogController类显示自定义弹窗。具体用法请参考自定义弹窗。

  • IntelliJ IDEA如何使用固定地址公网远程访问本地Mysql数据库

    IDEA作为Java开发最主力的工具,在开发过程中需要经常用到数据库,如Mysql数据库,但是在IDEA中只能连接本地数据库,有时候需要访问其他地方如家里或者公司的数据库,将无法访问,内网的局限性导致我们只能在同一网络访问,无法跨网络访问,所以,本例将介绍如何在异地也可以实现远程连接本地的数据库,这里我们需要用到一个工具,叫Cpolar.隧道创建成功后,点击左侧的状态——在线隧道列表,查看所生成的公网TCP协议的地址,该地址就是公网地址,我们可以在任何设备中的IDEA中都可以访问。