我得到下面的错误时,试图做一个选择通过一个存储过程在MySQL。
操作'='的排序规则(latin1_general_cs,IMPLICIT)和(latin1_general_ci,IMPLICIT)的非法混合
你知道哪里出了问题吗?
该表的排序规则为latin1_general_ci, where子句中的列的排序规则为latin1_general_cs。
我得到下面的错误时,试图做一个选择通过一个存储过程在MySQL。
操作'='的排序规则(latin1_general_cs,IMPLICIT)和(latin1_general_ci,IMPLICIT)的非法混合
你知道哪里出了问题吗?
该表的排序规则为latin1_general_ci, where子句中的列的排序规则为latin1_general_cs。
当前回答
这段代码需要放在运行SQL查询/数据库查询
SQL查询窗口
ALTER TABLE `table_name` CHANGE `column_name` `column_name` VARCHAR(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL;
请用合适的名称替换table_name和column_name。
其他回答
有时转换字符集可能是危险的,特别是在具有大量数据的数据库上。我认为最好的选择是使用“二进制”操作符:
e.g : WHERE binary table1.column1 = binary table2.column1
如果你遇到问题的列是“散列”,那么考虑以下…
如果“hash”是二进制字符串,你应该使用binary(…)数据类型。
如果“哈希”是一个十六进制字符串,你不需要utf8,应该避免这样做,因为字符检查等。例如,MySQL的MD5(…)会产生一个固定长度的32字节十六进制字符串。SHA1(…)给出一个40字节的十六进制字符串。这可以存储到CHAR(32)字符集ascii(或40的sha1)。
或者,更好的是,将UNHEX(MD5(…))存储为BINARY(16)。这样就把柱子的大小减少了一半。(然而,这确实使它不适合印刷。)SELECT十六进制(散列)…如果你想让它可读。
比较两个BINARY列没有排序规则问题。
这段代码需要放在运行SQL查询/数据库查询
SQL查询窗口
ALTER TABLE `table_name` CHANGE `column_name` `column_name` VARCHAR(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL;
请用合适的名称替换table_name和column_name。
Very interesting... Now, be ready. I looked at all of the "add collate" solutions and to me, those are band aid fixes. The reality is the database design was "bad". Yes, standard changes and new things gets added, blah blah, but it does not change the bad database design fact. I refuse to go with the route of adding "collate" all over the SQL statements just to get my query to work. The only solution that works for me and will virtually eliminate the need to tweak my code in the future is to re-design the database/tables to match the character set that I will live with and embrace for the long term future. In this case, I choose to go with the character set "utf8mb4".
因此,当您遇到“非法”错误消息时,这里的解决方案是重新设计数据库和表。这比听起来要简单快捷得多。甚至可能不需要导出数据并从CSV重新导入数据。更改数据库的字符集,并确保所有表的字符集都匹配。
使用这些命令来指导您:
SHOW VARIABLES LIKE "collation_database";
SHOW TABLE STATUS;
现在,如果您喜欢在这里或那里添加“collate”,并通过强制完全“重写”来增强代码,请听我的猜测。
一个可能的解决方案是将整个数据库转换为UTF8(另见此问题)。