@vincebowdren 上面的答案有效,我只是将其添加为格式化目的的答案:
CREATE TABLE `members` (
`id` int(11) DEFAULT NULL,
`lastname` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL
);
insert into members values (1, 'test6ë');
select id from members where lastname like 'test6e%';
产量
+------+
| 编号 |
+------+
| 1 |
+------+
并使用Latin1,
set names latin1;
CREATE TABLE `members2` (
`id` int(11) DEFAULT NULL,
`lastname` varchar(20) CHARACTER SET latin1 DEFAULT NULL
);
insert into members2 values (1, 'Renée');
select id from members2 where lastname like '%Renee%';
将产生:
+------+
| 编号 |
+------+
| 1 |
+------+
当然,OP 应该在应用程序(PHP)、连接(Linux 上的 MySQL 在 5.0 中默认为 latin1,但在 5.1 中默认为 UTF8)和字段数据类型中具有相同的字符集,以减少未知数。排序规则负责其余部分。
编辑:我写应该更好地控制一切,但以下也有效:
set names latin1;
select id from members where lastname like 'test6ë%';
因为,一旦设置了连接字符集,MySQL 就会在内部进行转换。在这种情况下,它将以某种方式转换并将 UTF8 字符串(来自 DB)转换为 latin1(来自查询)。
编辑2:一些怀疑要求我提供一个更有说服力的例子:
鉴于上述陈述,这里我做了更多。确保终端是 UTF8。
set names utf8;
insert into members values (5, 'Renée'), (6, 'Renêe'), (7, 'Renèe');
select members.id, members.lastname, members2.id, members2.lastname
from members inner join members2 using (lastname);
请记住,members
它在 utf8 和members2
latin1 中。
+--------+----------+------+----------+
| 编号 | 姓氏 | 编号 | 姓氏 |
+--------+----------+------+----------+
| 5 | 蕾妮 | 1 | 蕾妮 |
| 6 | 蕾妮 | 1 | 蕾妮 |
| 7 | 蕾妮 | 1 | 蕾妮 |
+--------+----------+------+----------+
这证明了正确的设置,排序规则为您完成了工作。