Home >Database >Mysql Tutorial >How to Solve MySQL's 'Illegal mix of collations' Error?

How to Solve MySQL's 'Illegal mix of collations' Error?

DDD
DDDOriginal
2024-12-22 13:37:17663browse

How to Solve MySQL's

Troubleshooting "Illegal mix of collations" Error in MySQL

When encountering the "Illegal mix of collations" error in MySQL during stored procedure execution, it's prudent to delve into the underlying causes.

Collation plays a critical role in MySQL, determining character set and sorting rules. A mismatch in collations between a table and a column referenced in the WHERE clause can lead to this error.

To resolve the issue, specify a shared collation for both columns involved in the comparison. The COLLATE clause allows you to override the default collation for a specific expression. For instance:

WHERE 'A' COLLATE latin1_general_ci = 'A' COLLATE latin1_general_cs

This query will still trigger the error as it compares strings with different collations. Instead, you should specify the shared collation explicitly:

SELECT * FROM table ORDER BY key COLLATE latin1_general_ci;

Alternatively, the BINARY operator can be utilized to force a binary comparison, ignoring collations:

SELECT * FROM table WHERE BINARY a = BINARY b;
SELECT * FROM table ORDER BY BINARY a;

Beware that casting columns for comparison, as in the case of the BINARY operator, may impact indexing performance. Refer to eggyal's comprehensive answer for further insights into MySQL collations.

The above is the detailed content of How to Solve MySQL's 'Illegal mix of collations' Error?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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