MySQL之模糊查询

 

先创建表:

create table student(
	id char(36) primary key,
	name varchar(8) not null,
	age int(3) default 0,
	mobile char(11),
	address varchar(150)
)
insert into student 
values ('9b4435ec-372c-456a-b287-e3c5aa23dff4','张三',24,'12345678901','北京海淀');
insert into student 
values ('a273ea66-0a42-48d2-a17b-388a2feea244','李%四',10,'98765432130',null);
insert into student 
values ('eb0a220a-60ae-47b6-9e6d-a901da9fe355','张李三',11,'18338945560','安徽六安');
insert into student 
values ('6ab71673-9502-44ba-8db0-7f625f17a67d','王_五',28,'98765432130','北京朝阳区');
insert into student 
values ('0055d61c-eb51-4696-b2da-506e81c3f566','王_五%%',11,'13856901237','吉林省长春市宽平区');
select * from student

where:指定查询过滤条件;

like:进行数据模糊查询

%:匹配0次或多次    


select * from student where name like '张%';#查询姓张的学生信息

select * from student where name like '%李%';#查询姓名中含有“李”字的学生信息

_:只匹配1次

select * from student where name like '张_';#查询两个字的张姓学生信息

    

escape:取消%或_字符的通配符特性

#查询姓名中含有%字符的学生信息
select * from student where name like '%#%%' escape '#'

#查询姓名中含有_字符的学生信息
select * from student where name like '%$_%' escape '$'

 

#查询姓名以%%结尾的学生信息
select * from student where name like '%q%q%' escape 'q';

#模糊查询
#% 可匹配0次或多次
select * from student where name like '张三%'
#_只能匹配一次
select * from student where name like '张三_'
select * from student where name like '张_'
select * from student where name like '%三'

select * from student where name like '%%%'
select * from student where name like '%A%%' escape 'A' #查询含有% 的name 
select * from student where name like '%A%' escape 'A'   #结果: 王_五%%  以%结尾的name
select * from student where name like '%A_%' escape 'A'  #含有_的name
select * from student where name like '%A五' escape 'A'   #  name 为 五结尾的

eccape后面不能是'%'或'_'

#查询全部学生信息
select * from student where name like '%_%_%' escape '_';
#相当于select * from student where name like '%%%'

 注意: escape后面单引号中只能是单个字符; escape后面可以是字母、#、$、,、\等字符;

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL支持多种方式进行前后模糊查询。其中一种常见的方法是使用通配符"%"来进行匹配。这个通配符示可以匹配任意长度的字符。比如,如果你想查询以"abc"开头的字符串,可以使用"abc%"。如果你想查询以"abc"结尾的字符串,可以使用"%abc"。如果你想查询包含"abc"的字符串,可以使用"%abc%"。这些都是使用通配符进行前后模糊查询的常见方式。除了通配符,MySQL还提供了其他内置函数进行模糊查询,比如locate、position和instr函数。这些函数可以根据指定的字符串在目标字符串中的位置来进行匹配。在实际使用中,选择合适的方法取决于具体的需求和数据量,可以根据实际情况选择最有效的方式进行前后模糊查询。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [mysql模糊查询的方法](https://blog.csdn.net/zuihongyan518/article/details/81131042)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [MySql模糊查询大全](https://blog.csdn.net/qq_46940224/article/details/125031647)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值