非法混合排序规则 MySQL 错误

发布于 2024-07-25 15:47:44 字数 380 浏览 8 评论 0原文

我在处理大量数据时遇到这个奇怪的错误...

Error Number: 1267

Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

SELECT COUNT(*) as num from keywords WHERE campaignId='12' AND LCASE(keyword)='hello again 昔 ã‹ã‚‰ ã‚ã‚‹ å ´æ‰€'

我该怎么解决这个问题? 我可以以某种方式转义字符串,这样就不会发生此错误,或者我是否需要以某种方式更改我的表编码,如果是这样,我应该将其更改为什么?

I'm getting this strange error while processing a large number of data...

Error Number: 1267

Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation '='

SELECT COUNT(*) as num from keywords WHERE campaignId='12' AND LCASE(keyword)='hello again 昔 ã‹ã‚‰ ã‚ã‚‹ å ´æ‰€'

What can I do to resolve this? Can I escape the string somehow so this error wouldn't occur, or do I need to change my table encoding somehow, and if so, what should I change it to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

驱逐舰岛风号 2024-08-01 15:47:45

将表的字符集改为utf8

更改表 your_table_name 转换为字符集 utf8

Change the character set of the table to utf8

ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8

无远思近则忧 2024-08-01 15:47:45

进行最上面答案中列出的更正后,更改服务器的默认设置。

在“/etc/my.cnf.d/server.cnf”或其所在位置将默认值添加到 [mysqld] 部分,如下所示:

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

来源: https://dev.mysql.com/doc/ refman/5.7/en/charset-applications.html

After making your corrections listed in the top answer, change the default settings of your server.

In your "/etc/my.cnf.d/server.cnf" or where ever it's located add the defaults to the [mysqld] section so it looks like this:

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

Source: https://dev.mysql.com/doc/refman/5.7/en/charset-applications.html

离不开的别离 2024-08-01 15:47:44
SET collation_connection = 'utf8_general_ci';

然后,对于您的数据库,

ALTER DATABASE your_database_name CHARACTER SET utf8 COLLATE utf8_general_ci;

ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

MySQL 有时会无缘无故地偷偷地在其中添加瑞典语。

SET collation_connection = 'utf8_general_ci';

then for your databases

ALTER DATABASE your_database_name CHARACTER SET utf8 COLLATE utf8_general_ci;

ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

MySQL sneaks swedish in there sometimes for no sensible reason.

音栖息无 2024-08-01 15:47:44
CONVERT(column1 USING utf8)

解决了我的问题。 其中column1是给我这个错误的列。

CONVERT(column1 USING utf8)

Solves my problem. Where column1 is the column which gives me this error.

记忆で 2024-08-01 15:47:44

您应该将表编码和连接编码设置为 UTF-8

ALTER TABLE keywords CHARACTER SET UTF8; -- run once

SET NAMES 'UTF8';
SET CHARACTER SET 'UTF8';

You should set both your table encoding and connection encoding to UTF-8:

ALTER TABLE keywords CHARACTER SET UTF8; -- run once

and

SET NAMES 'UTF8';
SET CHARACTER SET 'UTF8';
泼猴你往哪里跑 2024-08-01 15:47:44

使用以下语句来解决错误,

如果数据存在于表中,请小心您的数据并进行备份。

 ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

Use following statement for error

be careful about your data take backup if data have in table.

 ALTER TABLE your_table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
贪恋 2024-08-01 15:47:44

我发现使用 cast() 对我来说是最好的解决方案:

cast(Format(amount, "Standard") AS CHAR CHARACTER SET utf8) AS Amount

还有一个 convert() 函数。 更多详细信息此处

另一个资源此处

I found that using cast() was the best solution for me:

cast(Format(amount, "Standard") AS CHAR CHARACTER SET utf8) AS Amount

There is also a convert() function. More details on it here

Another resource here

凉城凉梦凉人心 2024-08-01 15:47:44

一般来说,最好的方法是更改​​表排序规则。 然而,我有一个旧的应用程序,并不能真正估计结果是否有副作用。 因此,我尝试以某种方式将字符串转换为解决排序问题的其他格式。
我发现有效的方法是通过将字符串转换为其字符的十六进制表示来进行字符串比较。 在数据库上,这是通过 HEX(column) 完成的。 对于 PHP,您可以使用此函数:

public static function strToHex($string)
{
    $hex = '';
    for ($i=0; $i<strlen($string); $i++){
        $ord = ord($string[$i]);
        $hexCode = dechex($ord);
        $hex .= substr('0'.$hexCode, -2);
    }
    return strToUpper($hex);
}

在进行数据库查询时,必须首先将原始 UTF8 字符串转换为 iso 字符串(例如使用 utf8_decode()(PHP 中)),然后在数据库中使用它。 由于排序规则类型,数据库内部不能包含 UTF8 字符,因此比较应该起作用,尽管这会更改原始字符串(转换 ISO 字符集中不存在的 UTF8 字符会导致 ? 或这些字符被完全删除)。 只需确保将数据写入数据库时​​,使用相同的 UTF8 到 ISO 转换即可。

In general the best way is to Change the table collation. However I have an old application and are not really able to estimate the outcome whether this has side effects. Therefore I tried somehow to convert the string into some other format that solved the collation problem.
What I found working is to do the string compare by converting the strings into a hexadecimal representation of it's characters. On the database this is done with HEX(column). For PHP you may use this function:

public static function strToHex($string)
{
    $hex = '';
    for ($i=0; $i<strlen($string); $i++){
        $ord = ord($string[$i]);
        $hexCode = dechex($ord);
        $hex .= substr('0'.$hexCode, -2);
    }
    return strToUpper($hex);
}

When doing the database query, your original UTF8 string must be converted first into an iso string (e.g. using utf8_decode() in PHP) before using it in the DB. Because of the collation type the database cannot have UTF8 characters inside so the comparism should work event though this changes the original string (converting UTF8 characters that are not existend in the ISO charset result in a ? or these are removed entirely). Just make sure that when you write data into the database, that you use the same UTF8 to ISO conversion.

思念绕指尖 2024-08-01 15:47:44

我的表格最初是用 CHARSET=latin1 创建的。 将表转换为 utf8 后,某些列未转换,但这并不明显。
您可以尝试运行 SHOW CREATE TABLE my_table; 并查看哪一列未转换,或者使用下面的查询修复有问题的列上的错误字符集(根据您的需要更改 varchar 长度以及 CHARSET 和 COLLATE):

 ALTER TABLE `my_table` CHANGE `my_column` `my_column` VARCHAR(10) CHARSET utf8 
 COLLATE utf8_general_ci NULL;

I had my table originally created with CHARSET=latin1. After table conversion to utf8 some columns were not converted, however that was not really obvious.
You can try to run SHOW CREATE TABLE my_table; and see which column was not converted or just fix incorrect character set on problematic column with query below (change varchar length and CHARSET and COLLATE according to your needs):

 ALTER TABLE `my_table` CHANGE `my_column` `my_column` VARCHAR(10) CHARSET utf8 
 COLLATE utf8_general_ci NULL;
缘字诀 2024-08-01 15:47:44

我的用户帐户没有更改数据库和表的权限,如本解决方案中的建议。

如果像我一样,您不关心 字符排序规则(您正在使用 ' =' 运算符),您可以应用反向修复。 在 SELECT 之前运行此命令:

SET collation_connection = 'latin1_swedish_ci';

My user account did not have the permissions to alter the database and table, as suggested in this solution.

If, like me, you don't care about the character collation (you are using the '=' operator), you can apply the reverse fix. Run this before your SELECT:

SET collation_connection = 'latin1_swedish_ci';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文