MySQL: Illegal mix of collations: both same collation

huangapple go评论132阅读模式
英文:

MySQL: Illegal mix of collations: both same collation

问题

我看到Stack Overflow上有很多关于类似问题的提问,但没有一个涉及到这个特定的细节。

我使用类似这样的方式创建了一个数据库:

  1. CREATE DATABASE db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;

我创建了一个具有相同字符集和校对规则的表:

  1. DROP TABLE IF EXISTS test;
  2. CREATE TABLE test (
  3. id int AUTO_INCREMENT PRIMARY KEY,
  4. data varchar(255)
  5. ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci ENGINE=INNODB;
  6. INSERT INTO test(data) VALUES ('apple'),('banana'),('cherry'),('date');

然后,我使用CTE连接了这个表:

  1. WITH cte(name) AS (
  2. SELECT cast('apple' as char(255))
  3. UNION ALL SELECT cast('cherry' as char(255))
  4. )
  5. SELECT * FROM cte JOIN test on cte.name=test.data;

我收到以下错误:

Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_520_ci,IMPLICIT)
for operation '='.

问题在于,数据库的默认校对规则与表的默认校对规则相同。

如果我在表的末尾不使用校对规则子句创建表,它是正常的。如果我使用nchar而不是char也可以正常工作。

我知道,为什么我不只是创建一个没有校对规则子句的表,或者使用nchar呢?问题是,如果我指定了校对规则,为什么char不起作用?

英文:

I see a number of questions on SO regarding something similar, but none of them addresses this particular detail.

I created a database using something like this:

  1. CREATE DATABASE db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;

I create a table with the same character set & collation:

  1. DROP TABLE IF EXISTS test;
  2. CREATE TABLE test (
  3. id int AUTO_INCREMENT PRIMARY KEY,
  4. data varchar(255)
  5. ) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci ENGINE=INNODB;
  6. INSERT INTO test(data) VALUES ('apple'),('banana'),('cherry'),('date');

I then join the table with a CTE:

  1. WITH cte(name) AS (
  2. SELECT cast('apple' as char(255))
  3. UNION ALL SELECT cast('cherry' as char(255))
  4. )
  5. SELECT * FROM cte JOIN test on cte.name=test.data;

I get the following error:

> Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_520_ci,IMPLICIT)
for operation '='.

The thing is, the default collation for the database is the same as that for the table.

If I create the table without the collation clause at the end, it’s fine. It also works if I use nchar instead of char.

I know, why don’t I just create the table without the collation clause, or use nchar? The question is why doesn’t it work with char if I specify the collation?

答案1

得分: 4

你的表没问题。

不兼容的是你的会话排序规则。

演示,使用你的示例表格和数据:

  1. mysql> WITH cte(name) AS (
  2. -> SELECT cast('apple' as char(255))
  3. -> UNION ALL SELECT cast('cherry' as char(255))
  4. -> )
  5. -> SELECT * FROM cte JOIN test on cte.name=test.data;
  6. ERROR 1267 (HY000): Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) and (utf8mb4_unicode_520_ci,IMPLICIT) for operation '=';

这是你遇到的错误,因为我的会话排序规则是utf8mb4_0900_ai_ci。

我可以更改会话排序规则并重新测试:

  1. mysql> SET NAMES utf8mb4 COLLATE utf8mb4_unicode_520_ci;
  2. mysql> WITH cte(name) AS (
  3. -> SELECT cast('apple' as char(255))
  4. -> UNION ALL SELECT cast('cherry' as char(255))
  5. -> )
  6. -> SELECT * FROM cte JOIN test on cte.name=test.data;
  7. +--------+----+--------+
  8. | name | id | data |
  9. +--------+----+--------+
  10. | apple | 1 | apple |
  11. | cherry | 3 | cherry |
  12. +--------+----+--------+
英文:

Your table is fine.

It's your session collation that is incompatible.

Demo, using your example table and data:

  1. mysql> WITH cte(name) AS (
  2. -> SELECT cast('apple' as char(255))
  3. -> UNION ALL SELECT cast('cherry' as char(255))
  4. -> )
  5. -> SELECT * FROM cte JOIN test on cte.name=test.data;
  6. ERROR 1267 (HY000): Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) and (utf8mb4_unicode_520_ci,IMPLICIT) for operation '='

That's the error you got, because my session collation is utf8mb4_0900_ai_ci.

I can change the session collation and test again:

  1. mysql> SET NAMES utf8mb4 COLLATE utf8mb4_unicode_520_ci;
  2. mysql> WITH cte(name) AS (
  3. -> SELECT cast('apple' as char(255))
  4. -> UNION ALL SELECT cast('cherry' as char(255))
  5. -> )
  6. -> SELECT * FROM cte JOIN test on cte.name=test.data;
  7. +--------+----+--------+
  8. | name | id | data |
  9. +--------+----+--------+
  10. | apple | 1 | apple |
  11. | cherry | 3 | cherry |
  12. +--------+----+--------+

huangapple
  • 本文由 发表于 2023年5月26日 07:33:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76336810.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定