Home > Database > Mysql Tutorial > body text

How to Resolve \'Illegal Mix of Collations\' Error in MySQL Queries?

Patricia Arquette
Release: 2024-11-03 23:59:30
Original
983 people have browsed it

How to Resolve

Illegal Character Sorting Resolution in MySql with Collation Error

In attempting to combine two subqueries in an SQL query, you may encounter the "Illegal mix of collations" error. This arises when the columns involved in comparisons or operations have different character set or collation settings.

To resolve this error, it's necessary to ensure that the columns used in the query have the same collation. To determine the columns affected, consult the following query:

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;
Copy after login

This will display the columns with the 'latin1_general_ci' collation. To resolve the error, convert the conflicting columns to the 'latin1_swedish_ci' collation using this query:

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

By rectifying the collation mismatch, the query can be executed successfully:

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
Copy after login

The above is the detailed content of How to Resolve \'Illegal Mix of Collations\' Error in MySQL Queries?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!