关于MySQL中的 like操作符详情

1、简介

当对未知或者说知道某一部分的值进行过滤时,可以使用like操作符;like操作符用于模糊匹配。

like支持两个通配符,它们分别是:

  • %通配符,用于匹配多个字符
  • _通配符,用于匹配单个字符

通配符根据其所处未知又分为六种匹配方式:

匹配方式 作用
%xx 表示右匹配,右边的xx字符需要完全相等,左边可以是任意字符,也可以没有字符
_xx 表示右匹配,右边的xx字符需要完全相等,左边可以是任意一个字符,必须是一个不能没有字符
xx% 表示左匹配,右边的xx字符需要完全相等,右边可以是任意字符,也可以没有字符
xx_ 表示左匹配,左边的xx字符需要完全相等,右边可以是任意一个字符,必须是一个不能没有字符
%xx% 表示中间匹配,中间必须完全相等,左右两边可以是任意字符,左右两边可以没有其他字符
xx 表示中间匹配,中间必须完全相等,左右两边可以是任意一个字符,左右两边必须是一个不能没有字符

2、正文

首先准备一张user表,ddl和表数据如下所示,可以直接复制使用。

set names utf8mb4;
set foreign_key_checks = 0;

-- ----------------------------
-- table structure for user
-- ----------------------------
drop table if exists `user`;
create table `user`  (
  `id` bigint(20) not null auto_increment comment '主键',
  `name` varchar(255) character set utf8 collate utf8_general_ci not null comment '用户名',
  `age` int(11) not null comment '年龄',
  `sex` smallint(6) not null comment '性别',
  primary key (`id`) using btree
) engine = innodb auto_increment = 8 character set = utf8 collate = utf8_general_ci row_format = dynamic;

-- ----------------------------
-- records of user
-- ----------------------------
insert into `user` values (1, '李子捌', 18, 1);
insert into `user` values (2, '张三', 22, 1);
insert into `user` values (3, '李四', 38, 1);
insert into `user` values (4, '王五', 25, 1);
insert into `user` values (5, '六麻子', 13, 0);
insert into `user` values (6, '田七', 37, 1);
insert into `user` values (7, '谢礼', 18, 1);

set foreign_key_checks = 1;
复制代码
数据的初始顺序如下所示:

mysql> select * from user;
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
|  2 | 张三   |  22 |   1 |
|  3 | 李四   |  38 |   1 |
|  4 | 王五   |  25 |   1 |
|  5 | 六麻子 |  13 |   0 |
|  6 | 田七   |  37 |   1 |
|  7 | 谢礼   |  18 |   1 |
+----+--------+-----+-----+
7 rows in set (0.00 sec)

2.1 %通配符

%通配符有三种匹配方式,分别是%xxxx% %xx% ,接下来演示三者的简单用法。

需求:

查询user表中姓氏为张的用户

语句:

mysql> select * from user where name like '张%';
+----+------+-----+-----+
| id | name | age | sex |
+----+------+-----+-----+
|  2 | 张三 |  22 |   1 |
+----+------+-----+-----+
1 row in set (0.00 sec)


需求:

查询user表中姓名以七结尾的用户

语句:

mysql> select * from user where name like '%七';
+----+------+-----+-----+
| id | name | age | sex |
+----+------+-----+-----+
|  6 | 田七 |  37 |   1 |
+----+------+-----+-----+
1 row in set (0.00 sec)

需求:

查询user表中姓名中包含李字符的用户

语句:

mysql> select * from user where name like '%李%';
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  1 | 李子捌 |  18 |   1 |
|  3 | 李四   |  38 |   1 |
+----+--------+-----+-----+
2 rows in set (0.00 sec)


2.2 _通配符

_通配符和%通配符的区别在于 _只匹配一个字符,并且必须匹配一个字符;而%可以匹配多个字符,甚至0个字符。

需求:

查询user表中姓氏为李,并且名字只有两个中文的用户

语句:

mysql> select * from user where name like '李_';
+----+------+-----+-----+
| id | name | age | sex |
+----+------+-----+-----+
|  3 | 李四 |  38 |   1 |
+----+------+-----+-----+
1 row in set (0.00 sec)



需求:

查询user表中名为三的用户

语句:

mysql> select * from user where name like '_三';
+----+------+-----+-----+
| id | name | age | sex |
+----+------+-----+-----+
|  2 | 张三 |  22 |   1 |
+----+------+-----+-----+
1 row in set (0.00 sec)


需求:

查询user表中姓名为三个子,并且第二个子为麻的用户

语句:

mysql> select * from user where name like '_麻_';
+----+--------+-----+-----+
| id | name   | age | sex |
+----+--------+-----+-----+
|  5 | 六麻子 |  13 |   0 |
+----+--------+-----+-----+
1 row in set (0.00 sec)


2.3 通配符使用注意事项

通配符非常强大,我相信很多人都经常使用通配符,但是字符串匹配往往并不是一件性能特别快的事情。因此我们在使用通配符的时候有一些注意事项需要时刻记住。

  • 能不用则不用的原则,不用能避免通配符带来的全部问题,所以如果其他操作符能查询出来,就不要使用like
  • 在使用通配符的地方,尽量缩小查询范围,如果有多个查询条件,应该考虑能否将通配符放置到其他过滤条件的后面
  • 特别注意通配符的选择,以及通配符的位置,可以参考六种匹配方式选择自己合适的

到此这篇关于mysqllike操作符详情的文章就介绍到这了,更多相关mysql之like操作符内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐