Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal mix of collations error in MySql

Just got this answer from a previous question and it works a treat!

SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount  FROM ratings WHERE month='Aug' GROUP BY username HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESC 

But when I stick this extra bit in it gives this error:

Documentation #1267 - Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (latin1_general_ci,IMPLICIT) for operation '='

SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount FROM  ratings WHERE month='Aug'  **AND username IN (SELECT username FROM users WHERE gender =1)** GROUP BY username HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESC 

The table is:

id, username, rating, month

like image 516
Oliver Avatar asked Aug 06 '09 22:08

Oliver


People also ask

What does illegal mix of collations mean?

An "illegal mix of collations" occurs when an expression compares two strings of different collations but of equal coercibility and the coercibility rules cannot help to resolve the conflict.

What are collations in MySQL?

A collation is a set of rules that defines how to compare and sort character strings. Each collation in MySQL belongs to a single character set. Every character set has at least one collation, and most have two or more collations. A collation orders characters based on weights.


1 Answers

Here's how to check which columns are the wrong collation:

SELECT table_schema, table_name, column_name, character_set_name, collation_name  FROM information_schema.columns  WHERE collation_name = 'latin1_general_ci'  ORDER BY table_schema, table_name,ordinal_position;  

And here's the query to fix it:

ALTER TABLE tbl_name CONVERT TO CHARACTER SET latin1 COLLATE 'latin1_swedish_ci'; 

Link

like image 155
Dean Rather Avatar answered Sep 19 '22 13:09

Dean Rather