Illegal mix of collations
Error 1267 (SQLSTATE HY000) is raised when a comparison or concatenation combines string values with incompatible collations. Collation governs how strings are compared and sorted; mixing collations (e.g., utf8mb4_general_ci vs utf8mb4_unicode_ci) in one expression is ambiguous.
- 1Columns from two tables have the same character set but different collations
- 2A literal string has a different collation than the column it is compared against
- 3A function or stored procedure returns a string with a different collation than expected
- 4Database, table, and column collations are inconsistent (created at different times with different defaults)
Joining two tables whose columns have different collations.
SELECT * FROM users u JOIN sessions s ON u.username = s.username; -- users.username: utf8mb4_general_ci -- sessions.username: utf8mb4_unicode_ci
expected output
ERROR 1267 (HY000): Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='
Fix 1
Use COLLATE to coerce one side of the comparison
WHEN As a quick fix or when you cannot alter the table schema.
SELECT * FROM users u JOIN sessions s ON u.username = s.username COLLATE utf8mb4_unicode_ci;
Why this works
COLLATE forces the expression to be evaluated using the specified collation. Choose the more precise collation (unicode_ci is generally preferred over general_ci).
Fix 2
Standardise collation across both columns
WHEN As the permanent fix — align collations at the schema level.
ALTER TABLE sessions MODIFY username VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Why this works
Making both columns use the same collation eliminates the mismatch. Set a consistent default at the database level to prevent recurrence.
Fix 3
Set a consistent database default collation
WHEN When creating a new database or when all tables can be migrated.
ALTER DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Why this works
The database collation is the default for new tables and columns. Changing it does not retroactively change existing columns.
✕ Mix utf8mb4_general_ci and utf8mb4_unicode_ci randomly
Leads to inconsistent sort orders and comparison results depending on which collation wins.
utf8mb4_uca1400_ai_ci is available and is the recommended modern collation for new databases.
MariaDB Server error code 1267 / ER_CANT_AGGREGATE_2COLLATIONS
MariaDB Character Sets and Collations ↗MariaDB Supported Collations ↗Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev