【问题标题】:MySQL - accent insensitive search with polish characters - UTF8MySQL - 带有波兰字符的重音不敏感搜索 - UTF8
【发布时间】:2013-11-05 18:40:15
【问题描述】:

我找到了很多关于排序规则和口音不敏感搜索的答案,阅读了大约 1000 篇关于这个问题的帖子和文章,但没有找到答案。

有人知道如何强制 mysql 搜索对所有波兰字符不敏感的重音吗?也许有人得到了那个(Debian)的编译整理文件?

请注意:

  • 将排序规则设置为utf8_general_ci 没有帮助。它不正确支持Ł。但它确实会破坏搜索顺序。
  • 将排序规则设置为utf8_unicode_ci 没有帮助。同上。
  • 无法编辑排序规则文件,因为它是多字节编码。并且必须编译多字节字符集。
  • 将所有不受支持的字母替换为受支持的字母不是解决方案。

我真的不明白为什么 MySQL 工作人员不威胁这是一个错误。很明显,确实如此,而且已经存在了很长时间。从 4.xx 开始,他们确实纠正了 Ś 字母......那为什么不 Ł 呢?!

我发现了一些对This MySQL functionality 的引用,但没有关于如何使用它的信息。我真的不明白那里写的是什么以及它是否可以帮助我。

测试:

mysql> show full columns from test;
+-------+--------------+----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type         | Collation      | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+--------------+----------------+------+-----+---------+-------+---------------------------------+---------+
| str   | varchar(255) | utf8_polish_ci | YES  |     | NULL    |       | select,insert,update,references |         |
+-------+--------------+----------------+------+-----+---------+-------+---------------------------------+---------+

mysql> insert into test values('Łomża');

...

mysql> select str from test where str like '%Łomża%'\G
*************************** 1. row ***************************
str: Łomża

mysql> select str from test where str like '%Łomza%'\G
Empty set (0.00 sec)

--

mysql> select str from test where str like '%Łomza%' collate utf8_general_ci\G
*************************** 1. row ***************************
str: Łomża

mysql> select str from test where str like '%Lomza%' collate utf8_general_ci\G
Empty set (0.00 sec)

--

mysql> select str from test where str like '%Łomza%' collate utf8_unicode_ci\G
*************************** 1. row ***************************
str: Łomża

mysql> select str from test where str like '%Lomza%' collate utf8_unicode_ci\G
Empty set (0.00 sec)

【问题讨论】:

  • 我不明白,我们用 MySQL 做搜索,即使是强 4 字节汉字表意文字,为什么 Ł 是个问题?
  • 询问 MySQL 开发人员。他们不认为这是一个错误!在过去的 5 年里被报告了 100000 次,官方的回答是这不是一个错误。请检查随附的示例。我已经编辑了问题。
  • 我不能帮助你,因为我没有抛光控制台。您使用 select str from test where str like '%Łomża%'\G;为什么你用 select str from test where str like '%Łomza%'\G 搜索,逻辑上返回一个空集?我将 str 插入为 utf8_general_ci,like 运算符似乎通过 PHP/PDO/MySQL 工作。
  • 不确定你的意思。通过这些测试,我想证明 collat​​e 不仅仅适用于Ł 字母。当我搜索ż 时,它可以与ż 以及z 一起正常工作。但是Ł 却没有。关于波兰控制台......它是带有英语 debian 的德国服务器 :)
  • 您是否会更新您的问题,并提供指向 MySQL 中有关“Ł”的已报告错误的链接?这似乎是读者需要了解的关键细节,当然他们必须评估这是否确实是 MySQL 方面的问题。

标签: mysql utf-8 collation


【解决方案1】:

我建议创建另一列用于在您的数据库中进行搜索,例如“str_search”。在数据库中向“str_search”插入字符串时,在 PHP 中创建并使用一个函数,如下所示:

 function convertPolishChars($phrase)
 {
    $phrase = str_replace("ą", "a", $phrase);
    $phrase = str_replace("Ą", "A", $phrase);

    $phrase = str_replace("ć", "c", $phrase);
    $phrase = str_replace("Ć", "C", $phrase);

    $phrase = str_replace("ę", "e", $phrase);
    $phrase = str_replace("Ę", "E", $phrase);

    $phrase = str_replace("ł", "l", $phrase);
    $phrase = str_replace("Ł", "L", $phrase);

    $phrase = str_replace("ń", "n", $phrase);
    $phrase = str_replace("Ń", "N", $phrase);

    $phrase = str_replace("ó", "o", $phrase);
    $phrase = str_replace("Ó", "O", $phrase);

    $phrase = str_replace("ś", "s", $phrase);
    $phrase = str_replace("Ś", "S", $phrase);

    $phrase = str_replace("ź", "z", $phrase);
    $phrase = str_replace("Ź", "Z", $phrase);

    $phrase = str_replace("ż", "z", $phrase);
    $phrase = str_replace("Ż", "Z", $phrase);

    return $phrase;
 }

"INSERT INTO test (str, str_search) VALUES ('Łomża', '" . convertPolishChars('Łomża') . "')"

在编写 SQL 查询时,请编写如下内容:

"SELECT str FROM test WHERE str_search like '%" . convertPolishChars('Łomża') . "%'"

这种方法将更快地执行您的查询字符串,而不是在 SQL 语句中进行任何转换。

确保索引您的“str_search”列。

对于较大的数据库,我建议使用 MATCH AGAINST 进行 FULLTEXT 搜索。 http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

【讨论】:

    【解决方案2】:

    我刚开始使用 MySql,“波兰语”问题的答案是整理utf8_unicode_520_ci(或utf8mb4_unicode_520_ci),其中l=ł=L=Ł - 其他口音也一样,不仅是波兰口音。

    没有 char 转换,没有 ascii 列,什么都没有……经过多年在 Sqlite 中搜索 ł/Ł 解决方案。

    【讨论】:

    • 你救了我。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 2012-01-28
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    相关资源
    最近更新 更多