当前位置:网站首页>MYSQL的binary解决mysql数据大小写敏感问题
MYSQL的binary解决mysql数据大小写敏感问题
2022-07-19 05:07:00 【西贝爷】
BINARY不是函数,是类型转换运算符,它用来强制它后面的字符串为一个二进制字符串,可以理解为在字符串比较的时候区分大小写
如下:
mysql> select binary 'ABCD'='abcd' COM1, 'ABCD'='abcd' COM2;
+--------+-----------+
| COM1 | COM2 |
+--------+-----------+
| 0 | 1 |
+---------+-----------+
1 row in set (0.00 sec)
____________________________________________________________
(仅仅有些而已!4.*以前)
因为有的MySQL特别是4.*以前的对于中文检索会有不准确的问题,可以在检索的时候加上binary。
建表:
create TABLE user (
id int(9) unsigned NOT NULL auto_increment,
username varchar(30) NOT NULL default '',
primary key (id)
)
插入数据:
insert into user (username) VALUES('美文');
insert into user (username) VALUES('美国项目');
insert into user (username) VALUES('李文');
insert into user (username) VALUES('老唐');
insert into user (username) VALUES('梦漂');
insert into user (username) VALUES('龙武');
insert into user (username) VALUES('夏');
例如:select * from user where username like '%夏%' ,结果七条记录都出来了,比较郁闷。
如果使用=而不是like的时候,select * from user where username = '夏' ,只出现一个结果。因为mysql 的LIKE操作是按照ASCII 操作的,所以LIKE的时候是可能有问题的。问题继续:如果再加上:
insert into user (username) VALUES('文');
insert into user (username) VALUES('唐');
还是使用select * from user where username = '夏' ,结果还是出现3条记录,又郁闷了。解决办法如下:
1.在create的时候就使用binary,而不是在query的时候加。
username varchar(30) BINARY NOT NULL default '', 如果表已经建好了,使用:
alter table user modify username varchar(32) binary; 来就该表的属性。
2.在query的时候加上binary,select * from user where username like binary '%夏%' ,就可以准确的查询出一条记录来。MYSQL的binary解决mysql数据大小写敏感问题
解决MSQL查询英文大小写不敏感问题:
1.可以将查询条件用binary()括起来。 比如:
select * from TableA where binary columnA ='aaa';
2.可以修改该字段的collation 为 binary
比如:
ALTER TABLE TABLENAME
MODIFY COLUMN COLUMNNAME VARCHAR(50) BINARY CHARACTER
SET utf8 COLLATE utf8_bin DEFAULT NULL;
边栏推荐
猜你喜欢
查询商品案例(利用数组新增方法操作数据)/重点
继承(子构造函数继承父构造函数中的属性)
83 reuse of local components [parent to child]
Simple construction of local image server
92 value transfer between brother components
基於C語言實現的學生管理系統
Supplement: ES6 knowledge points
10A polkadot substrate : 节点指标
69 simple chat dialog - expand - recall function
C语言实现 求解逆矩阵
随机推荐
92 value transfer between brother components
序列化和反序列化
文件包含漏洞
网安学习笔记-1 文件上传
无需编程,基于PostgreSQL零代码生成CRUD增删改查RESTful API接口
Why are there tripartite payments?
列表的渲染、过滤(筛选)、排序操作
Talking about wechat payment risk control
thinkphp6临时关闭布局
52 localstorage local storage
PHP 常用函数总结
如何搭建自己的第四方支付平台?
TS 接口
写了一点webapi的知识点从0到1
Wechat applet encapsulates custom tabbar, the sub page displays tabbar, and the main page can also be set (it is recommended to use the original tabbar of the applet), which is only for personal use
php生成的csv, 无法完整显示带前导0的数字
Supplement: ES6 knowledge points
C语言 --- 一元稀疏多项式运算
The difference between payment on behalf and distribution
除了Stripe,Paypal国内还能使用哪些海外支付接口