【问题标题】:Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT)归类 (utf8_unicode_ci,IMPLICIT) 和 (utf8_general_ci,IMPLICIT) 的非法混合
【发布时间】:2018-01-19 03:26:56
【问题描述】:

这是我的查询:

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 也不起作用。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    是的,这是因为 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 
    

    【讨论】:

    • Sooo 解决方案是使两个表具有相同的排序规则;理想情况下utf8mb4_unicode_ci
    • 仍然是同样的错误...我为所有表设置了排序规则utf8mb4_unicode_ci。有什么想法吗?
    • 注意到location_1 表是MyISAM,我将其更改为InnoDB,就像其他表一样。
    • 错误改了:#1253 - COLLATION 'utf8mb4_unicode_ci' is not valid for CHARACTER SET 'latin1'
    • @MartinAJ,将排序规则改为utf8_general_ci
    【解决方案2】:

    "Illegal mix of collat​​ion ... 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;
    

    干杯,

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 2010-11-03
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 2016-02-19
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多