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

目录

一、一般模糊查询

二、利用通配符查询

[1. _ 表示任意的单个字符](#1. _ 表示任意的单个字符)

[2. % 表示匹配任意多个任意字符](#2. % 表示匹配任意多个任意字符)

[3. [ ]表示筛选范围](#3. [ ]表示筛选范围)

[4. 查询包含通配符的字符串](#4. 查询包含通配符的字符串)


一、一般模糊查询

  1. 单条件查询
sql 复制代码
//查询所有姓名包含"张"的记录

select * from student where name like '张'
  1. 多条件查询
sql 复制代码
//查询所有姓名包含"张",地址包含四川的记录

select * from student where name like '张' and address like '四川'

//查询所有姓名包含"张",或者地址包含四川的记录

select * from student where name like '张' or address like '四川'

二、利用通配符查询

通配符:_ 、% 、[ ]

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

sql 复制代码
//查询所有名字姓张,字长两个字的记录

select * from student where name like '张_'

//查询所有名字姓张,字长三个字的记录

select * from student where name like '张__'

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

sql 复制代码
//查询所有名字姓张,字长不限的记录

select * from student where name like '张%'

//查询所有名字姓张,字长两个字的记录

select * from student where name like '张%'and len(name) = 2

3. [ ]表示筛选范围

sql 复制代码
//查询所有名字姓张,第二个为数字,第三个为燕的记录

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. 查询包含通配符的字符串

sql 复制代码
//查询姓名包含通配符%的记录

 select * from student where name like '%[%]%'                //通过[]转义

//查询姓名包含[的记录

 select * from student where name like '%/[%' escape '/'    //通过指定'/'转义

//查询姓名包含通配符[]的记录

 select * from student where name like '%/[/]%' escape '/'    //通过指定'/'转义

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

相关推荐
五岳6 小时前
分库分表数据源ShardingSphereDataSource的Connection元数据误用问题分析
java·mysql·爬坑
悄悄敲敲敲6 小时前
MySQL表的约束
数据库·mysql
鼠爷ねずみ6 小时前
SpringCloud前后端整体开发流程-以及技术总结文章实时更新中
java·数据库·后端·spring·spring cloud
九皇叔叔7 小时前
MySQL 数据库 Read View 详解
数据库·mysql·mvcc·read view
Elastic 中国社区官方博客8 小时前
Elasticsearch:圣诞晚餐 BBQ - 图像识别
大数据·数据库·elasticsearch·搜索引擎·ai·全文检索
cui_win8 小时前
Prometheus实战教程 - Redis 监控
数据库·redis·prometheus
JIngJaneIL9 小时前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
TG:@yunlaoda360 云老大9 小时前
华为云国际站代理商备份策略设置过程中遇到问题如何解决?
服务器·数据库·华为云
SelectDB9 小时前
Doris Catalog 已上线!性能提升 200x,全面优于 JDBC Catalog,跨集群查询迈入高性能分析时代
数据库·数据分析·apache
TAEHENGV9 小时前
进度跟踪模块 Cordova 与 OpenHarmony 混合开发实战
android·javascript·数据库