0
点赞
收藏
分享

微信扫一扫

【mysql学习】7.通配符匹配(模糊查询)

豆丁趣 2022-03-22 阅读 123

        这一篇文章中我们将使用LIKE操作符进行模糊查询,对数据进行复杂的过滤操作。


目录

准备工作

通配符过滤

百分号(%)通配符

下划线(_)通配符 

通配符注意事项 

总结


准备工作

create table food(id int primary key auto_increment,
		name varchar(32) not null,
		price float,
		fdescribe varchar(100));
insert into food values(null,'红烧肉','38','这个好吃');
insert into food values(null,'手抓羊肉','150','这个非常好吃');
insert into food values(null,'养生杂粮糕','45','这个好吃   ');
insert into food values(null,'四喜丸子','55',null);
insert into food values(null,'羊肉粉丝煲','100','真好吃');
insert into food values(null,'粤式白灼基围虾','66','真的好吃');
insert into food values(null,'杭椒牛柳','120','真美味');
insert into food values(null,'古法焖石鸡','80','真的好美味');
insert into food values(null,'宫保鸡丁a','31','这个好吃~');
insert into food values(null,'宫保鸡丁A','32','这个好吃~');


通配符过滤

        我们要想对数据进行模糊查询,那么我们需要使用关键字LIKELIKE位于WHERE的后面对数据进行过滤,like后面跟着的就是想要查询的数据片段,在mysql中,我们通过 和 来进行修饰。注意:LIKE只能用于文本字段(也就是字符串)


百分号(%)通配符

select * from food where name like '%肉';

 select * from food where name like '%a';

select * from food where fdescribe like '%好吃';

 select * from food where fdescribe like '%';


下划线(_)通配符 

select * from food where fdescribe like '%好吃';

select * from food where fdescribe like '_好吃';

 select * from food where fdescribe like '__好吃';


通配符注意事项 

  • 不要过度使用通配符,如果可以使用其他替代,那么我们应该就使用其他的操作符
  • 使用通配符,我们尽量往where的后面放,因为通配符匹配会花大量的时间进行过滤
  • 要注意通配符的位置,不要放错地方,位置错了,可能会得到数据,但往往是错的

总结

        通配符是一种重要的搜索方式,我们对数据库进行数据查找时会经常用到它,但是通配符的效率确实不快,我们也应当再必要时才使用,不要使用过度。

举报

相关推荐

0 条评论