简体   繁体   English

如何在 MySql 中进行重音敏感搜索

[英]How to conduct an Accent Sensitive search in MySql

I have a MySQL table with utf8 general ci collation.我有一个带有 utf8 通用 ci 排序规则的 MySQL 表。 In the table, I can see two entries:在表中,我可以看到两个条目:

abad阿巴德
abád阿巴德

I am using a query that looks like this:我正在使用如下所示的查询:

SELECT *  FROM `words` WHERE `word` = 'abád'

The query result gives both words:查询结果给出了两个词:

abad阿巴德
abád阿巴德

Is there a way to indicate that I only want MySQL to find the accented word?有没有办法表明我只希望 MySQL 找到重音词? I want the query to only return我希望查询只返回

abád阿巴德

I have also tried this query:我也试过这个查询:

SELECT *  FROM `words` WHERE BINARY `word` = 'abád'

It gives me no results.它没有给我任何结果。 Thank you for the help.感谢您的帮助。

If your searches on that field are always going to be accent-sensitive, then declare the collation of the field as utf8_bin (that'll compare for equality the utf8-encoded bytes) or use a language specific collation that distinguish between the accented and un-accented characters.如果您对该字段的搜索始终对重音敏感,则将该字段的排序规则声明为 utf8_bin(将比较 utf8 编码字节的相等性)或使用特定于语言的排序规则来区分重音符号和非- 重音字符。

col_name varchar(10) collate utf8_bin

If searches are normally accent-insensitive, but you want to make an exception for this search, try;如果搜索通常对重音不敏感,但您想为此搜索设置例外,请尝试;

WHERE col_name = 'abád' collate utf8_bin

In my version (MySql 5.0), there is not available any utf8 charset collate for case insensitive, accent sensitive searches.在我的版本 (MySql 5.0) 中,没有任何 utf8 字符集整理可用于不区分大小写、区分重音的搜索。 The only accent sensitive collate for utf8 is utf8_bin. utf8 唯一区分重音的校对是 utf8_bin。 However it is also case sensitive.但是,它也区分大小写。

My work around has been to use something like this:我的工作是使用这样的东西:

SELECT * FROM `words` WHERE LOWER(column) = LOWER('aBád') COLLATE utf8_bin

Accepted answer is good, but beware that you may have to use COLLATE utf8mb4_bin instead!接受的答案很好,但请注意,您可能必须改用 COLLATE utf8mb4_bin !

WHERE col_name = 'abád' collate utf8mb4_bin

Above fixes errors like:以上修复了以下错误:

MySQL said: Documentation 1253 - COLLATION 'utf8_bin' is not valid for CHARACTER SET 'utf8mb4' MySQL 说:文档 1253 - COLLATION 'utf8_bin' 对 CHARACTER SET 'utf8mb4' 无效

供以后参考的 MySQL 错误是http://bugs.mysql.com/bug.php?id=19567

Check to see if the database table collation type end with "_ci", This stands for case insensitive...检查数据库表整理类型是否以“_ci”结尾,这代表不区分大小写...

Change it to collation the same or nearest name without the "_ci" ...将其更改为不带“_ci”的相同或最接近的名称的排序规则...

For example... change "utf8_general_ci" to "utf8_bin" Mke例如...将“utf8_general_ci”更改为“utf8_bin”Mke

I was getting the same error.我遇到了同样的错误。

I've changed the collation of my table to utf8_bin (through phpMyAdmin) and the problem was solved.我已将表的排序规则更改为 utf8_bin(通过 phpMyAdmin),问题解决了。

Well, you just described what utf8_general_ci collation is all about (a, á, à, â, ä, å all equals to a in comparison).好吧,您刚刚描述了 utf8_general_ci 排序规则的全部内容(a、á、à、â、ä、å 都等于 a)。

There have also been changes in MySQL server 5.1 in regards to utf8_general_ci and utf8_unicode_ci so it's server version dependent too. MySQL 服务器 5.1 在 utf8_general_ci 和 utf8_unicode_ci 方面也发生了变化,因此它也依赖于服务器版本。 Better check the docs.最好检查文档。

So, If it's MySQL server 5.0 I'd go for utf8_unicode_ci instead of utf8_general_ci which is obviously wrong for your use-case.因此,如果它是 MySQL 服务器 5.0,我会选择 utf8_unicode_ci 而不是 utf8_general_ci,这对于您的用例来说显然是错误的。

That works for me for an accent insensitive and case insensitive search in MySql server 5.1 in a database in utf8_general_ci, where column is a LONGBLOB.这适用于我在 utf8_general_ci 中的数据库中的 MySql server 5.1 中进行不区分重音和不区分大小写的搜索,其中列是 LONGBLOB。

select * from words where '%word%' LIKE column collate utf8_unicode_ci

with

select * from words where'%word%' LIKE column collate utf8_general_ci

the result is case sensitive but not accent sensitive.结果区分大小写但不区分重音。

SELECT *  FROM `words` WHERE column = 'abád' collate latin1_General_CS 

(或您的整理,包括 cs)

You can try searching for the hex variable of the character, HEX() within mysql and use a similar function within your programming language and match these.您可以尝试在 mysql 中搜索字符的十六进制变量 HEX() 并在您的编程语言中使用类似的函数并匹配这些。 This worked well for me when i was doing a listing where a person could select the first letter of a person.当我做一个人可以选择一个人的第一个字母的列表时,这对我很有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM