各位用户为了找寻关于关于MySQL中的 like操作符详情的资料费劲了很多周折。这里教程网为您整理了关于关于MySQL中的 like操作符详情的相关资料,仅供查阅,以下为您介绍关于关于MySQL中的 like操作符详情的详细内容
1、简介
当对未知或者说知道某一部分的值进行过滤时,可以使用like操作符;like操作符用于模糊匹配。
like支持两个通配符,它们分别是:
%
通配符,用于匹配多个字符
_
通配符,用于匹配单个字符
通配符根据其所处未知又分为六种匹配方式:
2、正文
首先准备一张user表,ddl和表数据如下所示,可以直接复制使用。
? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43set
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 %通配符
%通配符有三种匹配方式,分别是%xx
、xx%
、 %xx%
,接下来演示三者的简单用法。
需求:
查询user
表中姓氏为张的用户
语句:
? 1 2 3 4 5 6 7mysql>
select
*
from
user
where
name
like
'张%'
;
+
----+------+-----+-----+
| id |
name
| age | sex |
+
----+------+-----+-----+
| 2 | 张三 | 22 | 1 |
+
----+------+-----+-----+
1 row
in
set
(0.00 sec)
需求:
查询user
表中姓名以七结尾的用户
语句:
? 1 2 3 4 5 6 7mysql>
select
*
from
user
where
name
like
'%七'
;
+
----+------+-----+-----+
| id |
name
| age | sex |
+
----+------+-----+-----+
| 6 | 田七 | 37 | 1 |
+
----+------+-----+-----+
1 row
in
set
(0.00 sec)
需求:
查询user
表中姓名中包含李字符的用户
语句:
? 1 2 3 4 5 6 7 8mysql>
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
表中姓氏为李,并且名字只有两个中文的用户
语句:
? 1 2 3 4 5 6 7mysql>
select
*
from
user
where
name
like
'李_'
;
+
----+------+-----+-----+
| id |
name
| age | sex |
+
----+------+-----+-----+
| 3 | 李四 | 38 | 1 |
+
----+------+-----+-----+
1 row
in
set
(0.00 sec)
需求:
查询user
表中名为三的用户
语句:
? 1 2 3 4 5 6 7mysql>
select
*
from
user
where
name
like
'_三'
;
+
----+------+-----+-----+
| id |
name
| age | sex |
+
----+------+-----+-----+
| 2 | 张三 | 22 | 1 |
+
----+------+-----+-----+
1 row
in
set
(0.00 sec)
需求:
查询user
表中姓名为三个子,并且第二个子为麻的用户
语句:
? 1 2 3 4 5 6 7mysql>
select
*
from
user
where
name
like
'_麻_'
;
+
----+--------+-----+-----+
| id |
name
| age | sex |
+
----+--------+-----+-----+
| 5 | 六麻子 | 13 | 0 |
+
----+--------+-----+-----+
1 row
in
set
(0.00 sec)
2.3 通配符使用注意事项
通配符非常强大,我相信很多人都经常使用通配符,但是字符串匹配往往并不是一件性能特别快的事情。因此我们在使用通配符的时候有一些注意事项需要时刻记住。
能不用则不用的原则,不用能避免通配符带来的全部问题,所以如果其他操作符能查询出来,就不要使用like 在使用通配符的地方,尽量缩小查询范围,如果有多个查询条件,应该考虑能否将通配符放置到其他过滤条件的后面 特别注意通配符的选择,以及通配符的位置,可以参考六种匹配方式选择自己合适的
到此这篇关于mysql
之like
操作符详情的文章就介绍到这了,更多相关mysql之like操作符内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://juejin.cn/post/7030568784448454663