Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE)

Discussion in 'MySQL' started by postcd, May 2, 2024.

  1. #1
    Hello, the CMS is running a SQL in MariaDB 10.6.17-MariaDB-cll-lve
    SELECT tagid, tagtext, canonicaltagid, dateline FROM tag WHERE tagtext = 'z�cpa';

    which results in:
    Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8mb4_general_ci,COERCIBLE) for operation '='

    tagtext is varchar(100) latin1_swedish_ci
    Server charset: cp1252 West European (latin1)

    Inside a CMS control panel, I have recently set an UTF-8 charset (because original ISO* one started displaying bad characters - maybe as a result of a SQL server update/upgrade).

    I have tried to change tag/tagtext column collation to utf8mb4_general_ci and it is no longer producing mentioned error, yet it "translates" z�cpa into z%E1cpa instead of zácpa. But tag list shows the string correctly now (maybe thanks to mine changed collation). So I guess that this is fixed.

    All tables has collation latin1_swedish_ci though. I am wondering if i should modify the CMS value from UTF-8 to something else? Any ideas please what that may be so there are no Illegal mix kind of errors? I am newbie.

    SELECT * FROM table ORDER BY key COLLATE latin1_general_ci;
    ALTER TABLE tbl_name CONVERT TO CHARACTER SET latin1 COLLATE 'latin1_swedish_ci';
     
    postcd, May 2, 2024 IP
  2. postcd

    postcd Well-Known Member

    Messages:
    1,043
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    190
    #2
    I think that what may need to be done is to change all tables collation to UTF-8. I have seen the problem where the data of the "posts" table were corrupted (question marks instead of a diacritics letters) as a result of a database export when the "posts" table had utf8 collation and also its rows but rest tables had old latin1_swedish_ci. What seemingly helps is to run in the database, SQL tab (after replacing "my_database_name"):

    
    SELECT CONCAT('ALTER TABLE ', TABLE_NAME, ' CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;')
    FROM information_schema.TABLES
    WHERE TABLE_SCHEMA = 'my_database_name' AND TABLE_TYPE != 'VIEW';
    
    Code (markup):
    Extra options button and then radio button to see full output. Copy it and remove erroneous lines. Valid one may look like: ALTER TABLE post CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    Paste edited output into your SQL tab of your database in a PHPMyAdmin and run it. It adjusted all tables and its rows collation to utf8mb3_unicode_ci. Somehow so far the text is not getting malformed by questionmarks.
     
    postcd, May 3, 2024 IP