在MySQL中不强调重音的搜索查询 [英] Accent insensitive search query in MySQL

查看:52
本文介绍了在MySQL中不强调重音的搜索查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any way to make search query accent insensitive?

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

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

示例词:toruń

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

找不到toruń".我该怎么办?

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

推荐答案

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

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

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

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

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.

或者,您可以将列复制到另一列,例如searchable_title,但是更改其排序规则.做这种事情实际上是很常见的,您可以在其中复制数据,但以某种略有不同的形式进行处理,这些形式针对某些特定的工作负载/用途进行了优化.您可以使用触发器作为使重复的列保持同步的好方法.如果建立索引,此方法可能具有良好的性能.

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.

注意-确保您的数据库确实包含那些字符,而不是html实体. 同样,连接的字符集也很重要.上面的示例假设已将其设置为utf8,例如通过设置名称,例如set names utf8

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_general_ci

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

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

这篇关于在MySQL中不强调重音的搜索查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆