首批通过分布式安全可靠测评,为关键业务系统打造
OceanBase 数据库 MySQL 模式查询如何区分大小写
更新时间:2026-05-26 09:46
适用版本
OceanBase 数据库所有版本。
问题现象
OceanBase 数据库 MySQL 模式,现在字段值里面保存的两个值只有大小写区分,发现 SQL 把两条都出来了吗,查询后不区分大小写。
mysql> select * from test where name = 'aaa';
+------+------+
| id | name |
+------+------+
| 3 | aaa |
| 3 | AAA |
+------+------+
2 row in set (0.01 sec)
问题原因
表的 collate 校对规则和用于判断的字符的 collate 校对规则不一致。
解决方法
OceanBase 数据库 V2.x 和 V3.x 版本通过加 binary 来临时解决。
mysql> select * from test where binary name = 'aaa';
+------+------+
| id | name |
+------+------+
| 3 | aaa |
+------+------+
1 row in set (0.01 sec)
mysql> select * from test where binary name = 'AAA';
+------+------+
| id | name |
+------+------+
| 3 | AAA |
+------+------+
1 row in set (0.00 sec)
OceanBase 数据库 V4.x 及以后版本直接更改表的 collate 规则即可。
mysql> ALTER TABLE cesi27 MODIFY COLUMN name VARCHAR(25) CHARACTER SET utf8 COLLATE utf8_bin;
Query OK, 0 rows affected (5.32 sec)
mysql> select * from cesi27 where name = 'xiaohei';
+----+---------+----------+
| id | name | classnum |
+----+---------+----------+
| 1 | xiaohei | class1 |
+----+---------+----------+
1 row in set (0.02 sec)
mysql> select * from cesi27 where name = 'XIAOHEI';
+----+---------+----------+
| id | name | classnum |
+----+---------+----------+
| 6 | XIAOHEI | class2 |
+----+---------+----------+
1 row in set (0.00 sec)