normalizing accented characters in MySQL queries

后端 未结 5 1865
-上瘾入骨i
-上瘾入骨i 2020-12-03 17:33

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

é, è, and ê

are all treated as \'e\', in querie

5条回答
  •  一向
    一向 (楼主)
    2020-12-03 17:36

    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.

    Using the MySQL Client, change it using SET NAMES:

    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;
    

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

    Example output:

    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)
    

    I know this question is pretty old but since Google led me here for a related question, I though it still deserves an answer :)

提交回复
热议问题