标准化 MySQL 查询中的重音字符

normalizing accented characters in MySQL queries(标准化 MySQL 查询中的重音字符)
本文介绍了标准化 MySQL 查询中的重音字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够进行标准化重音字符的查询,例如:

I'd like to be able to do queries that normalize accented characters, so that for example:

é, è, and ê

在使用="和喜欢"的查询中都被视为e".我有一行用户名字段设置为rené",我希望能够同时匹配rene"和rené"'.

are all treated as 'e', in queries using '=' and 'like'. I have a row with username field set to 'rené', and I'd like to be able to match on it with both 'rene' and 'rené'.

我正在尝试使用 MySQL 5.0.8 中的collat​​e"子句来做到这一点.我收到以下错误:

I'm attempting to do this with the 'collate' clause in MySQL 5.0.8. I get the following error:

mysql> select * from User where username = 'rené' collate utf8_general_ci;
ERROR 1253 (42000): COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'latin1'

FWIW,我的表是用以下方法创建的:

FWIW, my table was created with:

CREATE TABLE `User` (
  `id` bigint(19) NOT NULL auto_increment,
  `username` varchar(32) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `uniqueUsername` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=56790 DEFAULT CHARSET=utf8

推荐答案

错误的原因不是表格,而是您输入的字符集,即查询中的rené".行为取决于 character_set_connection 变量:

The reason for the error is not the table but the characterset of your input, i.e. the 'rené' in your query. The behaviour depends on the character_set_connection variable:

用于没有字符集介绍器的文字和用于数字到字符串转换的字符集.

The character set used for literals that do not have a character set introducer and for number-to-string conversion.

使用 MySQL 客户端,使用 SET NAMES 更改它:

Using the MySQL Client, change it using SET NAMES:

一个 SET NAMES 'charset_name' 语句相当于这三个语句:

A SET NAMES 'charset_name' statement is equivalent to these three statements:

SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET character_set_connection = charset_name;

(来自 http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html)

示例输出:

mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from User where username = 'rené' collate utf8_general_ci;
ERROR 1253 (42000): COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'latin1'

mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from User where username = 'rené' collate utf8_general_ci;
Empty set (0.00 sec)

或者,使用可以使用字符集介绍器"显式设置字符集:

Altenatively, use can explicitly set the character set using a 'character set introducer':

mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)

mysql> select * from User where username = _utf8'rené' collate utf8_general_ci;
Empty set (0.00 sec)

我知道这个问题已经很老了,但由于 Google 带我来这里回答一个相关问题,我认为它仍然值得回答:)

这篇关于标准化 MySQL 查询中的重音字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)