【问题标题】:How to deal with quotes and apostrophes for string comparison in MySQL so they match (collation)如何在 MySQL 中处理字符串比较的引号和撇号,以便它们匹配(排序规则)
【发布时间】:2011-05-22 00:22:06
【问题描述】:

MySQL 使用排序规则进行字符串比较,因为某些字符应该匹配

示例:

SELECT 'é' = 'e' COLLATE utf8_unicode_ci;
SELECT 'oe' = 'œ' COLLATE utf8_unicode_ci; 

都返回真

现在,我怎样才能对引号 (') 和撇号 (') 做同样的事情

这不是同一个字符,写“it's”或“l'oiseau”(法语)时使用的正确字符都是撇号。

事实是 utf8_general_ci 或 utf8_unicode_ci 都没有整理它们。

简单的解决方案是将所有内容存储在引号中,并在用户进行搜索时替换所有撇号,但这是错误的。

真正的解决方案是创建基于 utf8_unicode_ci 的自定义排序规则并将两者标记为等效,但这需要编辑 XML 配置文件并重新启动数据库,这并不总是可行的。

你会怎么做?

【问题讨论】:

  • 所以你想要一个“真正的解决方案”而不是你已经提到的那个? ;-) 我的建议是创建一个自定义排序规则(当然,假设您已经搜索了 mysql 文档和其他 3rd 方来源,以获得已经满足您需求的现有排序规则)

标签: mysql quotes collation diacritics apostrophe


【解决方案1】:

自定义排序规则似乎是最合适的,但如果不可能,也许您可​​以调整搜索以使用正则表达式。它并不完全理想,但在某些情况下可能有用。至少它允许您以正确的格式存储数据(无需替换引号),并且只需对搜索查询本身进行替换:

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   |
+----+-----------------------------------------------------+

【讨论】:

    猜你喜欢
    • 2016-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多