Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL: Illegal mix of collations: both same collation

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:

CREATE DATABASE db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

I then join the table with a CTE:

WITH cte(name) AS (
    SELECT cast('apple' as char(255))
    UNION ALL SELECT cast('cherry' as char(255))
)
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?

>Solution :

Your table is fine.

It’s your session collation that is incompatible.

Demo, using your example table and data:

mysql> WITH cte(name) AS (
    ->     SELECT cast('apple' as char(255))
    ->     UNION ALL SELECT cast('cherry' as char(255))
    -> )
    -> SELECT * FROM cte JOIN test on cte.name=test.data;
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:

mysql> SET NAMES utf8mb4 COLLATE utf8mb4_unicode_520_ci;

mysql> WITH cte(name) AS (
    ->     SELECT cast('apple' as char(255))
    ->     UNION ALL SELECT cast('cherry' as char(255))
    -> )
    -> SELECT * FROM cte JOIN test on cte.name=test.data;
+--------+----+--------+
| name   | id | data   |
+--------+----+--------+
| apple  |  1 | apple  |
| cherry |  3 | cherry |
+--------+----+--------+
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading