Troubleshooting “Illegal mix of collations” error in mysql

前端 未结 17 2383
一生所求
一生所求 2020-11-22 08:11

Am getting the below error when trying to do a select through a stored procedure in MySQL.

Illegal mix of collations (latin1_general_cs,IMPLICIT) and

17条回答
  •  粉色の甜心
    2020-11-22 08:28

    Adding my 2c to the discussion for future googlers.

    I was investigating a similar issue where I got the following error when using custom functions that recieved a varchar parameter:

    Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and 
    (utf8_general_ci,IMPLICIT) for operation '='
    

    Using the following query:

    mysql> show variables like "collation_database";
        +--------------------+-----------------+
        | Variable_name      | Value           |
        +--------------------+-----------------+
        | collation_database | utf8_general_ci |
        +--------------------+-----------------+
    

    I was able to tell that the DB was using utf8_general_ci, while the tables were defined using utf8_unicode_ci:

    mysql> show table status;
        +--------------+-----------------+
        | Name         | Collation       |
        +--------------+-----------------+
        | my_view      | NULL            |
        | my_table     | utf8_unicode_ci |
        ...
    

    Notice that the views have NULL collation. It appears that views and functions have collation definitions even though this query shows null for one view. The collation used is the DB collation that was defined when the view/function were created.

    The sad solution was to both change the db collation and recreate the views/functions to force them to use the current collation.

    • Changing the db's collation:

      ALTER DATABASE mydb DEFAULT COLLATE utf8_unicode_ci;
      
    • Changing the table collation:

      ALTER TABLE mydb CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
      

    I hope this will help someone.

提交回复
热议问题