非法混合排序规则 MySQL 错误
我在处理大量数据时遇到这个奇怪的错误...
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
将表的字符集改为utf8
Change the character set of the table to utf8
进行最上面答案中列出的更正后,更改服务器的默认设置。
在“/etc/my.cnf.d/server.cnf”或其所在位置将默认值添加到 [mysqld] 部分,如下所示:
来源: 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:
Source: https://dev.mysql.com/doc/refman/5.7/en/charset-applications.html
然后,对于您的数据库,
MySQL 有时会无缘无故地偷偷地在其中添加瑞典语。
then for your databases
MySQL sneaks swedish in there sometimes for no sensible reason.
解决了我的问题。 其中column1是给我这个错误的列。
Solves my problem. Where column1 is the column which gives me this error.
您应该将表编码和连接编码设置为
UTF-8
:
You should set both your table encoding and connection encoding to
UTF-8
:and
使用以下语句来解决错误,
如果数据存在于表中,请小心您的数据并进行备份。
Use following statement for error
be careful about your data take backup if data have in table.
我发现使用
cast()
对我来说是最好的解决方案:还有一个
convert()
函数。 更多详细信息此处另一个资源此处
I found that using
cast()
was the best solution for me:There is also a
convert()
function. More details on it hereAnother resource here
一般来说,最好的方法是更改表排序规则。 然而,我有一个旧的应用程序,并不能真正估计结果是否有副作用。 因此,我尝试以某种方式将字符串转换为解决排序问题的其他格式。
我发现有效的方法是通过将字符串转换为其字符的十六进制表示来进行字符串比较。 在数据库上,这是通过
HEX(column) 完成的。
对于 PHP,您可以使用此函数:在进行数据库查询时,必须首先将原始 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: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.我的表格最初是用 CHARSET=latin1 创建的。 将表转换为 utf8 后,某些列未转换,但这并不明显。
您可以尝试运行
SHOW CREATE TABLE my_table;
并查看哪一列未转换,或者使用下面的查询修复有问题的列上的错误字符集(根据您的需要更改 varchar 长度以及 CHARSET 和 COLLATE):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):我的用户帐户没有更改数据库和表的权限,如本解决方案中的建议。
如果像我一样,您不关心 字符排序规则(您正在使用 ' =' 运算符),您可以应用反向修复。 在 SELECT 之前运行此命令:
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: