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','吉林省长春市宽平区');

下面开始模糊查询:like

百分号%

#%匹配0次或多次
select * from student where name like '张%';#查询姓张的学生信息
select * from student where name like '%李%';#查询姓名中含有“李”字的学生信息

查询张的结果
查询李的结果
所谓匹配就是百分号对应的字符,可以对应零个、一个或多个

下划线_

#_下划线只能且必须匹配一次
select * from student where name like '张_'

在这里插入图片描述
下划线_必须匹配一个字符,且只能匹配一个字符,张立三就查不出来

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

#escape:取消%或_字符的通配符特性
#查询姓名中含有%字符的学生信息
select * from student where name like '%#%%' escape '#'
#查询姓名中含有%字符的学生信息
select * from student where name like '%$_%' escape '$'
#查询姓名以%%结尾的学生信息
select * from student where name like '%_%_%' escape '_';

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

escape后面单引号中只能是单个字符;
escape后面可以是字母、#、$、,、\、_等字符;使这个字符后面的一个字符失去其特性

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值