Illegal mix of collations when running stored procedure - mysql

I have a very long stored procedure and I keep getting the following error:
Illegal mix of collations (utf8mb4_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation '='
It's occurring on some databases but not others but when I run SHOW TABLE STATUS, all of the collations are set to latin1_swedish_ci
Does anyone have any advice on how to debug this? Is there a way to determine where in the stored procedure the error is coming from so I can try to pinpoint the issue?

In MySQL, collation can be set at different levels.
Database level
Table level
Column level
Apart from that collation can also be set at the server and connection level to MySQL.
Therefore, Check the collation type on various levels and make sure that they have the same collation. You can check the collation by:
Database collation
SELECT default_collation_name FROM information_schema.schemata WHERE schema_name = '<schema_name>';
Table collation
SELECT table_schema, table_name, table_collation FROM information_schema.tables WHERE table_schema = '<schema_name>';
Column collation
SELECT table_schema, table_name, column_name, collation_name FROM information_schema.columns WHERE table_schema = '<schema_name>';
Collation for the server, connection, and database
SHOW VARIABLES LIKE 'collation%';

Related

Get a Table's Character Set

In MySQL, I can get a table's name, engine and collation like so:
SELECT TABLE_NAME, TABLE_SCHEMA, ENGINE, TABLE_COLLATION
FROM information_schema.tables
WHERE table_name = 'tbl_name';
But how how do I get a table's character set, not just collation? Is it possible to get it from information_schema.tables?
Each collation is used for only one character set, so it's not necessary to record the character set in the INFORMATION_SCHEMA.TABLES. The table collation is enough to indicate both the collation and the character set unambiguously.
You can check INFORMATION_SCHEMA.COLLATIONS or INFORMATION_SCHEMA.COLLATION_CHARACTER_SET_APPLICABILITY to get the mapping from a given collation to its character set.
Try running this:
SELECT default_character_set_name FROM information_schema.SCHEMATA S WHERE schema_name = "DBNAME";

MySql VIEW created with charset latin1 all configs are set to utf8

I'm trying to create a simple view but I'm getting error because the view is created with latin1 instead of utf8.
The View looks something like this:
create or replace view
my_view
as
select * from my_table
group by some_field
collate utf8_unicode_ci
;
The error I'm getting is:
COLLATION 'utf8_unicode_ci' is not valid for CHARACTER SET 'latin1'
What I did, was check multiple configuration options:
Global
show variables like "%char%";
character_set_client,utf8
character_set_connection,utf8
character_set_database,utf8
character_set_filesystem,binary
character_set_results,utf8
character_set_server,utf8
character_set_system,utf8
The table I'm using to create the view:
SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_COLLATION
FROM INFORMATION_SCHEMA.TABLES
where TABLE_NAME in ('my_table');
;
def,my_database,my_table,utf8_unicode_ci
The Columns of that table:
SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, COLLATION_NAME
FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME in ('my_table');
def,my_database,my_table,id,null
def,my_database,my_table,active,null
def,my_database,my_table,title,utf8_unicode_ci
I had the same issue in an MySQL database, where I had Umlauts/special charaters within a CASE like this (reduced sample):
CASE
WHEN cfs.value = "ungeklärt" THEN "" -- the character 'ä' was an issue for me here
ELSE cfs.value
END AS "Category"
The error popped up for me, when savin this statement as a view.
Can you check if it solves the issue for you, when you set the names accordingly first? In my case this was latin1 instead of utf8 due to an old legacy database.
set names latin1;
If I understood your case correclty it would be this for you instead:
set names utf8;

Information Schema showing two character encodings for column

I'm in the process of migrating a MySQL database from the utf8 character set to uft8mb4, following this guide (https://mathiasbynens.be/notes/mysql-utf8mb4). For one of the tables I updated (table1) I get weird output from the information_schema. table1 has four columns, each listed below:
data_store VARCHAR(24)
data_group VARCHAR(24)
source_count INT
load_count INT
I have validated that only 4 columns appear through SELECT * on the table. However, running the following query on information_schema produces odd output.
SELECT column_name, character_set_name FROM information_schema.COLUMNS
WHERE table_name = "table1";
COLUMN_NAME CHARACTER_SET_NAME
--------------------------------------------------------------------------------------
data_store utf8
data_group utf8
source_count <null>
load_count <null>
data_store utf8mb4
data_group utf8mb4
source_count <null>
load_count <null>
I don't see duplicate rows (with differing character sets) for any other table that I have updated and am at a loss in regards to what is wrong and/or how to fix it. Any help would be much appreciated!
Notes: I believe I could just remove the unwanted columns from information_schema, but I'm not sure if this would break anything.
I think you're seeing the columns from different table1 tables in several different database schemas.
Try this to verify that claim.
SELECT table_schema, column_name, character_set_name
FROM information_schema.COLUMNS
WHERE table_name = 'table1'
Try this query to filter by the current database.
SELECT column_name, character_set_name
FROM information_schema.COLUMNS
WHERE table_name = 'table1'
AND table_schema = DATABASE()
Do not try to alter the INFORMATION_SCHEMA database in any way. Don't delete rows, don't add columns, or anything else. It's supposed to be readonly. But sometimes it isn't, and altering it can trash your MySQL instance. Don't ask me how I know that. :-)

Issue in a query with left join

Following is my query in which I am getting results from two different tables, but its giving me following error kindly let me know what i did wrong:
Error: #1267 - Illegal mix of collations (utf8_general_ci,COERCIBLE) and (latin1_swedish_ci,IMPLICIT) for operation '='
SELECT MD5( pre_quiz.qid ),
pre_quiz.quiz_title,
pre_quiz.quiz_desc,
pre_course.cname
FROM pre_quiz
LEFT JOIN pre_course ON
MD5( pre_course.cid ) = pre_quiz.quiz_course_id
WHERE pre_quiz.createdby = 'user'
ORDER BY pre_quiz.quiz_title
Check the collation type of each table, and make sure that they have the same collation.
After that check also the collation type of each table field that you have use in operation.
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';
Edit
To change the collation of a column
ALTER TABLE MyTable ALTER COLUMN Column1 [TYPE] COLLATE [NewCollation]
Source

PHP Mysql - Illegal mix of collations when using CONCAT

Iv'e been trying to concat two columns from my table and perform a LIKE on them
Here is the mysql query:
SELECT * FROM stops WHERE CONCAT_WS(name, ' ', desc) LIKE
'%This%' AND CONCAT_WS(name, ' ', desc) LIKE '%is%' AND
CONCAT_WS(name, ' ', desc) LIKE '%a%'
AND CONCAT_WS(name, ' ', desc) LIKE '%test%'
When I run it via my php code or phpmyadmin I get the following error:
Illegal mix of collations (utf8_bin,NONE) and
(utf8_general_ci,COERCIBLE) for operation 'like'
Iv'e been checking similar questions about this topic and saw that a lot of
the answers told to change the collation of the database.
I already changed all database, tables and fields collcation to utf8_general_ci
When I run the same query exactly in Navicat it works just fine.
Am I missing something here?
Thanks!
set the database and tables -> character to utf8
ALTER DATABASE db CHARACTER SET utf8 COLLATE utf8_general_ci
ALTER TABLE table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci