简体   繁体   English

MySQL是否在查询中转换UTF-8字符(而不是结果)并去除重音?

[英]Is MySQL converting UTF-8 characters in my query (not the results), stripping accents?

I've some records in a DB where one of the VARCHAR fields may contain accented letters. 我在数据库中有一些记录,其中VARCHAR字段之一可能包含带重音的字母。 If I do the following query using the CLI MySQL client I get 1 row returned, which is correct: 如果使用CLI MySQL客户端执行以下查询,则会返回1行,这是正确的:

SELECT site_id, site_name FROM tbl_site WHERE site_name LIKE '%ém%'

However, using some PHP (PDO) to do the same query returns all the rows that contain 'em', but not the one row that contains 'ém'. 但是,使用某些PHP(PDO)进行相同的查询将返回所有包含'em'的行,而不返回包含'ém'的一行。 Even more strangely, MySQL query logging shows that the query contains 'é', not 'e'. 更奇怪的是,MySQL查询记录显示查询包含“é”,而不是“ e”。

Any ideas what might be going on here? 任何想法可能在这里发生什么?

This depends on the character set and collation of the database, table or column. 这取决于字符集和数据库,表或列的排序规则 Are you performing the query against the same table? 您是否要对同一张表执行查询?

Could you post the results of 您可以发布以下结果吗?

SHOW CREATE TABLE <yourTable>;

Additionally, you could specify the desired collation on a query level: 此外,您可以在查询级别指定所需的排序规则:

SELECT site_id, site_name FROM tbl_site WHERE site_name LIKE '%ém%' collate utf8_bin;
-- same as
SELECT site_id, site_name FROM tbl_site WHERE BINARY site_name LIKE '%ém%';

(Note: You should very carefully evaluate the perfomance of such queries) (注意:您应该非常仔细地评估此类查询的性能)

utf8_general_ci排序规则不区分大小写不区分重音。

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

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