Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal mix of collations for operation 'concat'

Tags:

sql

mysql

I'm trying to execute this concat query in mysql

SELECT CONCAT(if(fName,fName,''),Name) 
From Student

Error:

#1271 - Illegal mix of collations for operation 'concat'

like image 366
ahmed saud Avatar asked Oct 13 '11 11:10

ahmed saud


People also ask

What is illegal collation mix?

So what is an "illegal mix of collations"? 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 is MySQL collate?

A MySQL collation is a well-defined set of rules which are used to compare characters of a particular character-set by using their corresponding encoding. Each character set in MySQL might have more than one collation, and has, at least, one default collation. Two character sets cannot have the same collation.


2 Answers

This is due to collections difference, you can solve by converting the two strings or columns to one collection say UTF8

CONCAT(CAST(fName AS CHAR CHARACTER SET utf8),CAST('' AS CHAR CHARACTER SET utf8))

This will solve :)

you can check more about casting in MySQL here MySQL Casting

like image 111
Anas Naguib Avatar answered Oct 13 '22 06:10

Anas Naguib


The charsets and/or collations you use in your connection do not match the charset/collation in your table.

There are 4 solutions:

1- Change the charset in your connection:

//find out the charset used in your table.
SHOW TABLES LIKE 'student'
//set the server charset to match
SET NAMES 'charset_name' [COLLATE 'collation_name']

2- Change the charset used in your table to match the server charset:

//find out the charset used in the server
SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';
//Change the charset used in the table
ALTER TABLE student ......

3- Change the default charset settings and restart MySQL

Edit My.ini and replace the character_set_* options, so they match your tables.

4- Change the charset settings for your connection

You client can override the charset and collation settings.
If it does not option 1 or 3 should fix your issue, but if the connection overrides these settings, you need to check the connection-string and edit the charset/collation settings to match your database.

Some advice:

Find a charset. I recommend UTF8 and a collation: I recommend utf8_general_ci. And use those consistantly everywhere.

like image 38
Johan Avatar answered Oct 13 '22 04:10

Johan