Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal mix of collations (utf8_unicode_ci,COERCIBLE) and (utf8_general_ci,COERCIBLE) for operation '='

Tags:

casting

mysql

When I run the following query:

CREATE ALGORITHM = UNDEFINED VIEW d_view_galerias AS (
SELECT id, titulo, 'foto' AS tipo, '' AS embed
FROM d_galeria_fotos
)
UNION (

SELECT id, titulo, 'video' AS tipo, embed
FROM d_galeria_videos
)

I get the error:

Illegal mix of collations (utf8_unicode_ci,COERCIBLE) and (utf8_general_ci,COERCIBLE) for operation '='

"tipo" is getting as utf8_unicode, but the other fields are as utf8_general ... how to make a cast, convert?

like image 312
user2321359 Avatar asked Aug 20 '13 17:08

user2321359


People also ask

What is the difference between utf8_general_ci and utf8_unicode_ci?

In short: utf8_unicode_ci uses the Unicode Collation Algorithm as defined in the Unicode standards, whereas utf8_general_ci is a more simple sort order which results in "less accurate" sorting results. If you don't care about correctness, then it's trivial to make any algorithm infinitely fast.

What is utf8_general_ci?

utf8_general_ci is a legacy collation that does not support expansions, contractions, or ignorable characters. It can make only one-to-one comparisons between characters.

What is utf8mb4_unicode_ci?

utf8mb4_unicode_ci , which uses the Unicode rules for sorting and comparison, employs a fairly complex algorithm for correct sorting in a wide range of languages and when using a wide range of special characters.


2 Answers

The error message is rather confusing as it specifies operation = -- which is not obvious from the query you posted but is caused by the UNION query which select only distinct values. So using implicitly an equality comparaison.

Anyway, you can always force the collation of a column using the COLLATE clause. Here is a an example, assuming you want to change the collation of the column tipo:

SELECT id, titulo, 'foto' COLLATE utf8_general_ci AS tipo

... 
UNION SELECT id, titulo, 'video' COLLATE utf8_general_ci AS tipo, ...
like image 148
Sylvain Leroux Avatar answered Nov 15 '22 09:11

Sylvain Leroux


I had a similar problem. What I did was to isolate the comparison in the where clause that's causing the error. I ran the CONVERT function on the value I to compare with into the collation of the table.

`field` = CONVERT(value USING charset_of_table)

See this post for more details and examples on using CONVERT

like image 45
David Lartey Avatar answered Nov 15 '22 11:11

David Lartey