深入探究Mysql模糊查询是否区分大小写

吾爱主题 阅读:137 2024-04-02 08:03:09 评论:0

前言

近期,一直在忙着写一个小小的个人博客项目,在实现 “全局搜索” 功能时,发现mysql模糊查询语句有点儿神奇(本小白刚刚步入编程阶段,所以可能让大家见笑了,哈哈哈),有时候 mysql模糊查询语句区分大小写,有时候 mysql查询语句又不区分度大小写,于是,做了很多次实验,得出以下结论(可能有不对的地方,欢迎大佬们可以指出我的不足,也欢迎小伙伴们可以一起讨论问题!)

先来介绍一下collate,collate通常是和数据编码(charset)相关的,一般来说每种charset都有多种它所支持的collate,并且每种charset都指定一种collate为默认值。例如latin1编码的默认collate为latin1_swedish_cigbk编码的默认collate为gbk_chinese_ciutf8mb4编码的默认值为utf8mb4_general_ci

chsrset collate
latin1 latin1_swedish_ci
gbk gbk_chinese_ci
utf8mb4 utf8mb4_general_ci
utf8 utf8_bin

有兴趣的小伙伴,可以在navicate等数据库可视化工具中,自己尝试一下:




注意:mysql中有utf8utf8mb4两种编码,但是mysql中的utf8最多只能支持3bytes长度的字符编码,所以,建议大家还是选择utf8mb4编码格式;

解决方法一

如图所示,为了便于区分,让小伙伴们更好地理解,在此,本小白建立了两个一模一样的数据库,其中一个采用utf8编码格式,另外一个采用utf8mb4格式:

区分大小写

建表语句也采用utf8编码格式:

建表语句

blog_test : utf8编码格式

?
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 set names utf8; set foreign_key_checks = 0;   -- ---------------------------- -- table structure for t_blog -- ---------------------------- drop table if exists `t_blog`; create table `t_blog`  (    `id` bigint (20) not null auto_increment,    `appreciation` bit (1) not null ,    `comment_tabled` bit (1) not null ,    `content` longtext character set utf8 collate utf8_bin null ,    `create_time` datetime(0) null default null ,    `first_picture` varchar (255) character set utf8 collate utf8_bin null default null ,    `flag` varchar (255) character set utf8 collate utf8_bin null default null ,    `published` bit (1) not null ,    `recommend` bit (1) not null ,    `share_statement` bit (1) not null ,    `title` varchar (255) character set utf8 collate utf8_bin null default null ,    `update_time` datetime(0) null default null ,    `views` int (11) null default null ,    `type_id` bigint (20) null default null ,    `user_id` bigint (20) null default null ,    `description` varchar (255) character set utf8 collate utf8_bin null default null ,    primary key (`id`) using btree,    index `fk292449gwg5yf7ocdlmswv9w4j`(`type_id`) using btree,    index `fk8ky5rrsxh01nkhctmo7d48p82`(`user_id`) using btree,    constraint `fk292449gwg5yf7ocdlmswv9w4j` foreign key (`type_id`) references `t_type` (`id`) on delete restrict on update restrict ,    constraint `fk8ky5rrsxh01nkhctmo7d48p82` foreign key (`user_id`) references `t_user` (`id`) on delete restrict on update restrict ) engine = innodb auto_increment = 14 character set = utf8 collate = utf8_bin row_format = dynamic ;

数据库内容

查询结果 select * from t_blog where title like ‘%html%';

select * from t_blog where title like ‘%html%';

结论:

mysql采用utf8编码格式,模糊查询区分大小写

不区分大小写

建表语句

建表语句也采用utf8mb4编码格式:

blog_test2 : utf8mb4编码格式

?
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 set names utf8mb4; set foreign_key_checks = 0;   -- ---------------------------- -- table structure for t_blog -- ---------------------------- drop table if exists `t_blog`; create table `t_blog`  (    `id` bigint (20) not null auto_increment,    `appreciation` bit (1) not null ,    `comment_tabled` bit (1) not null ,    `content` longtext character set utf8mb4 collate utf8mb4_general_ci null ,    `create_time` datetime(0) null default null ,    `first_picture` varchar (255) character set utf8mb4 collate utf8mb4_general_ci null default null ,    `flag` varchar (255) character set utf8mb4 collate utf8mb4_general_ci null default null ,    `published` bit (1) not null ,    `recommend` bit (1) not null ,    `share_statement` bit (1) not null ,    `title` varchar (255) character set utf8mb4 collate utf8mb4_general_ci null default null ,    `update_time` datetime(0) null default null ,    `views` int (11) null default null ,    `type_id` bigint (20) null default null ,    `user_id` bigint (20) null default null ,    `description` varchar (255) character set utf8mb4 collate utf8mb4_general_ci null default null ,    primary key (`id`) using btree,    index `fk292449gwg5yf7ocdlmswv9w4j`(`type_id`) using btree,    index `fk8ky5rrsxh01nkhctmo7d48p82`(`user_id`) using btree,    constraint `fk292449gwg5yf7ocdlmswv9w4j` foreign key (`type_id`) references `t_type` (`id`) on delete restrict on update restrict ,    constraint `fk8ky5rrsxh01nkhctmo7d48p82` foreign key (`user_id`) references `t_user` (`id`) on delete restrict on update restrict ) engine = innodb auto_increment = 14 character set = utf8mb4 collate = utf8mb4_general_ci row_format = dynamic ;

数据库内容

查询结果 select * from t_blog where title like ‘%html%';

select

* from t_blog where title like ‘%html%';

结论

mysql采用utf8mb4编码格式,模糊查询不区分大小写

解决方法二

 区分大小写

方法

单独指定所需字段(比如,在此处 我所需要区分大小写的字段为表 ‘t_blog' 中的 ‘title' 字段)为 ‘utf8' 编码格式,collate 为“utf8_bin”;

?
1 alter table t_blog change `title` `title` varchar (255) character set utf8 collate utf8_bin null default null ;

结论

select * from t_blog where title like ‘%html%';

select * from t_blog where title like ‘%html%';

由上述执行结果可以看出,此方法可以使模糊查询语句区分大小写

 不区分大小写

方法

单独指定所需字段(比如,在此处 我所需要不区分大小写的字段为表 ‘t_blog' 中的 ‘title' 字段)为 ‘utf8mb4' 编码格式,collate 为“utf8mb4_bin”:

?
1 alter table t_blog change `title` `title` varchar (255) character set utf8mb4 collate utf8mb4_bin null default null ;

结论 select * from t_blog where title like ‘%html%';

select * from t_blog where title like ‘%html%';

由上述执行结果可以看出,此方法可以使模糊查询语句不区分大小写

解决办法三

区分大小写

方法

如图所示,当前 ‘t_blog' 表格的 ‘title' 字段的 collate 为 ‘utf8mb4_general_ci' ,不区分大小写

修改字段为binary:

?
1 alter table t_blog change `title` `title` varchar (255) binary null default null ;
?
1 alter table `t_blog` modify column `title` varchar (255) binary null default null ;

两种修改方式选择一种既可

结论

select * from t_blog where title like ‘%html%';

select * from t_blog where title like ‘%html%';

由上述执行结果可以看出,修改字段为binary,可以使模糊查询语句区分大小写

解决办法四

区分大小写

方法

如图所示,当前 ‘t_blog' 表格的 ‘title' 字段的 collate 为 ‘utf8mb4_general_ci' ,不区分大小写

查询语句字段前面加binary:

?
1 select * from `t_blog` where binary `title` like '%html%' ;

结论

select * from t_blog where title like ‘%html%';

select * from t_blog where binary title like ‘%html%';

由上述执行结果可以看出,在查询语句字段前面加binary,可以使模糊查询语句区分大小写

解决办法五

(特别特别不推荐,尽量不要使用)

找到mysql相应版本下的my.ini文件,本小白对应的8.0.17版本;


在文件最后一行加上下面一句话:

?
1 lower_case_table_names=1

注意:0:区分大小写,1:不区分大小写

重启mysql

本小白还有很多地方讲解的不够清楚明了,还请各位大佬指教!想更深入了解的可以参考大佬的博客(膜拜膜拜)mysql区分大小写

总结

到此这篇关于mysql模糊查询是否区分大小写的文章就介绍到这了,更多相关mysql模糊查询大小写内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_44694201/article/details/117785085

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容