MySQL查询默认不区分大小

mysql的查询默认是不区分大小写的。

看个例子:

可以看到使用如下语句,查询到了同一条记录。

select * from tba where name = 'test';
select * from tba where name = 'TEST';

mysql的表查询大小写是否敏感,取决于字段的collate设置。

我们看下这张表的charset和collate。

charset是utf8,而utf8的默认collate规则是utf8_general_ci。(可通过show charset命令查看。)

*_ci的含义是case insensitive,即不区分大小写。

如果我们想使查询区分大小写。有两种方法:

1、查询条件前binary;

mysql> select * from tba where binary name = 'test';
+----+------+-----+------------+
| id | name | age | addtime    |
+----+------+-----+------------+
|  1 | test |  12 | 4294967295 |
+----+------+-----+------------+
1 row in set (0.00 sec)

mysql>  select * from tba where binary name = 'TEST';
Empty set (0.00 sec)

2、可以通过修改字段的collate为utf8_bin;

utf8_bin表示将字符串中的每一个字符用二进制数据存储,区分大小写。

ALTER TABLE tba MODIFY COLUMN name VARCHAR(25) CHARACTER  SET utf8 COLLATE utf8_bin;
mysql> ALTER TABLE tba MODIFY COLUMN name VARCHAR(20) CHARACTER  SET utf8 COLLATE utf8_bin;
Query OK, 2 rows affected (0.04 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from tba where name = 'test';
+----+------+-----+------------+
| id | name | age | addtime    |
+----+------+-----+------------+
|  1 | test |  12 | 4294967295 |
+----+------+-----+------------+
1 row in set (0.00 sec)

mysql> select * from tba where name = 'TEST';
Empty set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值