5
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)
4

3 回答 3

2

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

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

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

有关详细信息,请参阅参考手册页字符串文字字符集和排序规则

于 2013-05-22T18:35:10.893 回答
2

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)
于 2013-05-23T11:16:42.713 回答
0

我知道这已经晚了,但我希望它对某人有所帮助。我一直收到同样的错误,我知道我的字符集和排序规则很好。

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

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

于 2015-10-23T17:41:31.547 回答