1267ERRORTier 2 — Caution✅ HIGH confidenceIllegal mix of collations
What this means
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.
Why it happens
- 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)
How to reproduce
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_ciFix 1: Use COLLATE to coerce one side of the comparison
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
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 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.
What not to do
Mix utf8mb4_general_ci and utf8mb4_unicode_ci randomly
Why it's wrong: Leads to inconsistent sort orders and comparison results depending on which collation wins.
Version notes
MariaDB 10.6+utf8mb4_uca1400_ai_ci is available and is the recommended modern collation for new databases.Sources
📚 Official docs: https://mariadb.com/kb/en/setting-character-sets-and-collations/
🔧 Source ref: MariaDB Server error code 1267 / ER_CANT_AGGREGATE_2COLLATIONS
📖 Further reading: MariaDB Character Sets and Collations
📖 Further reading: MariaDB Supported Collations
Confidence assessment
✅ HIGH confidence
Stable.
See also
📄 Reference pages