COLLATION'utf8_general_ci'对CHARACTER SET'二进制'无效?

时间:2013-05-22 18:02:49

标签: mysql character-encoding collation

mysql> SELECT LOCATE("n", "München") COLLATE utf8_general_ci;
ERROR 1253 (42000): COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'binary'

如何摆脱此错误?

我已经尝试过(复制和粘贴):

$ mysql -u admin -p $DATABASE
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.69 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT LOCATE("n", "München") COLLATE utf8_general_ci;
ERROR 1253 (42000): COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'binary'
mysql> SET NAMES utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT LOCATE("n", "München") COLLATE utf8_general_ci;
ERROR 1253 (42000): COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'binary'
mysql> SELECT LOCATE(_utf8"n", _utf8"München") COLLATE utf8_general_ci;
ERROR 1253 (42000): COLLATION 'utf8_general_ci' is not valid for CHARACTER SET 'binary'
mysql> SHOW VARIABLES LIKE "character_set_database";
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| character_set_database | utf8  |
+------------------------+-------+
1 row in set (0.00 sec)

3 个答案:

答案 0 :(得分:2)

可能服务器已使用默认字符集二进制进行编译,因此字符串文字正在被解释为,或者客户端设置为在与服务器通信时使用二进制模式。您可以通过调用SET NAMES utf8来更改客户端和连接字符集(尽管如果您的SQL语句是从PHP发出的,则不建议这样做,因为PHP将有自己的用于设置连接字符集的命令)。请参阅MySQL参考手册中的Connection Character Sets and Collations

或者,您可以使用“介绍人”明确指定用于 LOCATE 函数中字符串文字的字符集,例如:

LOCATE(_utf8"n", _utf8"München")

有关详细信息,请参阅参考手册页Character String Literal Character Set and Collation

答案 1 :(得分:2)

我的示例sets the collation of the return value中的COLLATE LOCATE,其结果类型为binary

设置参数的排序规则:

mysql> SELECT LOCATE(_utf8"n" COLLATE utf8_general_ci,
                     _utf8"München" COLLATE utf8_general_ci) AS locate;
+--------+
| locate |
+--------+
|      3 |
+--------+
1 row in set (0.00 sec)

我的动机实际上是找出MySQL是否进行整理 在搜索子字符串时考虑到。不幸的是 的。查看第二个命令的结果:

mysql> SELECT LOCATE(_utf8"ü" COLLATE utf8_general_ci,
                     _utf8"München" COLLATE utf8_general_ci) AS locate;
+--------+
| locate |
+--------+
|      2 |
+--------+
1 row in set (0.00 sec)

mysql> SELECT LOCATE(_utf8"u" COLLATE utf8_general_ci,
                     _utf8"München" COLLATE utf8_general_ci) AS locate;
+--------+
| locate |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)

使用临时表进行测试(在WHERE子句中考虑整理,但不在。{ LOCATE):

mysql> CREATE TEMPORARY TABLE test
       (text VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_general_ci);
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO test VALUES("München");
Query OK, 1 row affected (0.00 sec)

mysql> SELECT text FROM test WHERE text LIKE "%u%";
+---------+
| text    |
+---------+
| München |
+---------+
1 row in set (0.00 sec)

mysql> SELECT LOCATE("u", text) AS locate FROM test WHERE text LIKE "%u%"; 
+--------+
| locate |
+--------+
|      0 |
+--------+
1 row in set (0.01 sec)

答案 2 :(得分:0)

我知道这已经很晚了,但我希望它有所帮助。我一直得到同样的错误,我知道我的字符集和校对很好。

检查' @'您声明中不属于的符号。我正在测试我的存储过程作为带变量的select语句,然后在创建存储过程时忘记删除' @'符号。不用说,我觉得很傻。

我也知道这个问题在这个问题上似乎并非如此,但这是我的第一篇SO帖子,而且我没有足够的代表去做其他事情,所以我道歉。