The problem
I use the native function FIND_IN_SET with a variable @myVar for the list of string, like :
FIND_IN_SET(col, @myVar)
If I set @myVar from a direct value :
SET @myVAr = 'one,two,three';
... FIND_IN_SET(col, @myVar)
=> It works great
If I set @myVar from a function returning the concatenated values from one column of a table :
SET @myVAr = '';
SELECT @myVAr := getStringOfValues();
... FIND_IN_SET(col, @myVar)
=> I get Erreur SQL (1267) : Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation 'find_in_set'
So, I do i get rid of this error ?
It is important to note that I don't want to change the collations of my table. I wish I could change the collation of my variable so it match the collation of my column, but I cannot force it with the keyword "COLLATE" during its initialization :
SET @myVar= '' COLLATE utf8_unicode_ci;
=> I get Erreur SQL (1253) : COLLATION 'utf8_unicode_ci' is not valid for CHARACTER SET 'utf8mb4'
Useful informations
All my table and their columns have the collation utf8_unicode_ci
SHOW VARIABLES LIKE '%char%'
character_set_client = utf8mb4
character_set_connection = utf8mb4
character_set_database = utf8
character_set_filesystem = binary
character_set_results = utf8mb4
character_set_server = latin1
character_set_system = utf8
SHOW VARIABLES LIKE 'coll%'
collation_connection = utf8mb4_general_ci
collation_database = utf8_general_ci
collation_server = latin1_swedish_ci
SHOW CREATE FUNCTION getStringOfValues
character_set_client = utf8mb4
collation_connection = utf8m4_general_ci
Database Collation = utf8_general_ci
The SQL code (anonymous and simplified version)
-- Function to return as a string the result from a SELECT in my_table_1
DELIMITER $$
CREATE FUNCTION getStringOfValues()
RETURNS TEXT
-- Variables to build the string of values
DECLARE curr_value VARCHAR(255) DEFAULT '';
DECLARE string_of_values TEXT DEFAULT '';
-- Variables to enter and exit the loop over values
DECLARE done INT DEFAULT 0;
DECLARE my_cursor CURSOR FOR SELECT col FROM my_table_1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN my_cursor;
my_loop:LOOP
FETCH my_cursor INTO curr_value;
IF done THEN
LEAVE my_loop;
ELSE
SET string_of_values = CONCAT(curr_value, ',', string_of_values);
END IF;
END LOOP;
CLOSE my_cursor;
-- Remove the trailing comma and quote the string. Result in something like 'one,two,three'
RETURN QUOTE(TRIM(TRAILING ',' FROM string_of_values));
$$
DELIMITER ;
SET @myVAr = 'one,two,three';
-- The statement below will WORK
SELECT * FROM my_table_2 WHERE FIND_IN_SET(col, @myVAr);
SELECT @myVAr := getStringOfValues();
-- The statement below will NOT WORK as there is a 'Illegal mix of collations'
SELECT * FROM my_table_2 WHERE FIND_IN_SET(col, @myVAr);