MySQL 使用排序规则进行字符串比较,因为某些字符应该匹配
Exemple:
SELECT 'é' = 'e' COLLATE utf8_unicode_ci;
SELECT 'oe' = 'œ' COLLATE utf8_unicode_ci;
两者都返回 true
现在,我怎样才能对引号 (') 和撇号 (') 做同样的事情
这不是同一个字符,写“it's”或“l'oiseau”(法语)时使用的正确字符都是撇号。
事实是 utf8_general_ci 或 utf8_unicode_ci 都不会整理它们。
简单的解决方案是将所有内容存储在引号中,并在用户进行搜索时替换所有撇号,但这是错误的。
真正的解决方案是创建一个基于 utf8_unicode_ci 的自定义排序规则并将两者标记为等效,但这需要编辑 XML 配置文件并重新启动数据库,而这并不总是可行。
你会怎么做?
自定义排序规则似乎是最合适的,但如果不可能,也许您可以定制搜索以使用正则表达式。它并不完全理想,但在某些情况下可能有用。至少它允许您以正确的格式存储数据(无需替换引号),并且只需对搜索查询本身进行替换:
INSERT INTO mytable VALUES
(1, 'Though this be madness, yet there is method in ''t'),
(2, 'Though this be madness, yet there is method in ’t'),
(3, 'There ’s daggers in men’s smiles'),
(4, 'There ’s daggers in men''s smiles');
SELECT * FROM mytable WHERE data REGEXP 'There [\'’]+s daggers in men[\'’]+s smiles';
+----+--------------------------------------+
| id | data |
+----+--------------------------------------+
| 3 | There ’s daggers in men’s smiles |
| 4 | There ’s daggers in men's smiles |
+----+--------------------------------------+
SELECT * FROM mytable WHERE data REGEXP 'Though this be madness, yet there is method in [\'’]+t';
+----+-----------------------------------------------------+
| id | data |
+----+-----------------------------------------------------+
| 1 | Though this be madness, yet there is method in 't |
| 2 | Though this be madness, yet there is method in ’t |
+----+-----------------------------------------------------+
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)