MySQL 中的重音不敏感搜索查询

发布于 2024-12-22 23:17:09 字数 191 浏览 0 评论 0原文

有什么方法可以使搜索查询重音不敏感吗?

列和表的排序规则是 utf8_polish_ci,我不想更改它们。

示例单词:toruń

select * from pages where title like '%torun%'

找不到“toruń”。我怎样才能做到这一点?

Is there any way to make search query accent insensitive?

the column's and table's collation are utf8_polish_ci and I don't want to change them.

example word : toruń

select * from pages where title like '%torun%'

It doesn't find "toruń". How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漆黑的白昼 2024-12-29 23:17:09

您可以在 sql 查询中在运行时更改排序规则,

...where title like '%torun%' collate utf8_unicode_ci 

但要注意,在运行时动态更改排序规则会放弃 mysql 使用索引的可能性,因此大型表的性能可能会很糟糕。

或者,您可以将该列复制到另一列,例如 searchable_title,但更改其排序规则。实际上,做这种类型的事情很常见,您复制数据,但以稍微不同的形式保存数据,并针对某些特定的工作负载/目的进行了优化。您可以使用触发器作为保持重复列同步的好方法。如果建立索引,此方法有可能表现良好。

注意 - 确保您的数据库确实具有这些字符而不是 html 实体。
此外,连接的字符集也很重要。上面假设它设置为 utf8,例如,通过 设置名称< /a> 就像设置名称utf8

如果没有,你需要一个introducer 用于文字值

...where title like _utf8'%torun%' collate utf8_unicode_ci 

,当然,单引号中的值实际上必须是 utf8 编码的,即使 sql 查询的其余部分不是。

You can change the collation at runtime in the sql query,

...where title like '%torun%' collate utf8_unicode_ci 

but beware that changing the collation on the fly at runtime forgoes the possibility of mysql using an index, so performance on large tables may be terrible.

Or, you can copy the column to another column, such as searchable_title, but change the collation on it. It's actually common to do this type of stuff, where you copy data but have it in some slightly different form that's optimized for some specific workload/purpose. You can use triggers as a nice way to keep the duplicated columns in sync. This method has the potential to perform well, if indexed.

Note - Make sure that your db really has those characters and not html entities.
Also, the character set of your connection matters. The above assumes it's set to utf8, for example, via set names like set names utf8

If not, you need an introducer for the literal value

...where title like _utf8'%torun%' collate utf8_unicode_ci 

and of course, the value in the single quotes must actually be utf8 encoded, even if the rest of the sql query isn't.

又爬满兰若 2024-12-29 23:17:09

这在极端情况下不起作用,但请尝试将列排序规则更改为 UFT8 utf8_unicode_ci。那么重音字符将等于其非重音字符。

This wont work in extreme circumstances, but try to change the column collation to UFT8 utf8_unicode_ci. Then accented characters will be equal to their non-accented counterparts.

孤独难免 2024-12-29 23:17:09

您可以尝试 SOUNDEX:

http://dev.mysql。 com/doc/refman/5.0/en/string-functions.html#function_soundex

这会根据两个字符串的发音进行比较。但这显然带来了更多的结果。

You could try SOUNDEX:

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_soundex

This compares two string by how they sound. But this obviously delivers many more results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文