不合法的字符集混合 (utf8_unicode_ci, IMPLICIT) 和 (utf8_general_ci, IMPLICIT)。

5

以下是我的查询语句:

INSERT INTO location_province(name, country)   
SELECT child.name
      ,location_country.id
  FROM location_1 child
 INNER JOIN location_1 parent
    ON child.parent_id = parent.id
 INNER JOIN location_country
    ON location_country.name = parent.name
 WHERE child.location_type = 1

出现了以下错误:

#1267 - 在操作“=”时字符集不一致(utf8_unicode_ci,IMPLICIT)和(utf8_general_ci,IMPLICIT)

出了什么问题,我该怎么解决?


注意:在查询末尾添加COLLATE utf8_unicode_ci也无法解决该问题。

2个回答

11

是的,那是因为JOIN ON子句以及每个错误都与ON条件涉及的列的排序规则不匹配有关。这些列的排序规则必须匹配。 我是指下面的行:

ON child.parent_id = parent.id  ------ 1
 INNER JOIN location_country
    ON location_country.name = parent.name ------ 2

请检查您要加入的表格并进行验证

在加入时更改排序方式,例如

 INNER JOIN location_country
    ON location_country.name collate utf8_general_ci = parent.name collate utf8_general_ci 

所以解决方法是使两个表的排序方式相同;最好使用utf8mb4_unicode_ci - Martin
仍然是同样的错误...我已经为所有表设置了utf8mb4_unicode_ci排序规则。有任何想法吗? - Martin AJ
注意到 location_1 表是 MyISAM,我已将其更改为像其他表一样的 InnoDB - Martin AJ
错误已更改:#1253 - COLLATION 'utf8mb4_unicode_ci' 不适用于 CHARACTER SET 'latin1' - Martin AJ
@MartinAJ,请将排序规则更改为utf8_general_ci - Rahul
仍然出现#1253-COLLATION'utf8_general_ci'对于CHARACTER SET'latin1'无效的错误。您能告诉我问题具体在哪里吗? - Martin AJ

2

"Illegal mix of collation ... for operator =" 异常是由where子句中的文本列(例如VARCHAR类型)引起的。为了演示此问题,请尝试创建两个具有不同排序规则的相同表,然后将它们连接起来:

create table t1_gen (label varchar(10) collate utf8_general_ci);
insert into t1_gen values ('foobar');

create table t2_uni (label varchar(10) collate utf8_unicode_ci);
insert into t2_uni values ('foobar');

以下是会导致完全相同异常的连接。两个列的排序规则确实不匹配:

select * from t1_gen, t2_uni where t1_gen.label = t2_uni.label;

如果您更改where子句中字段的顺序,异常将发生变化。

select * from t1_gen, t2_uni where t2_uni.label = t1_gen.label;

为了使此查询起作用,我们在where子句中明确地向不匹配的列添加了校对规则:
select * from t1_gen, t2_uni where t1_gen.label collate utf8_unicode_ci = t2_uni.label;

cheers,


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接