Troubleshooting “Illegal mix of collations” error in mysql

前端 未结 17 2274
一生所求
一生所求 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:35

    Solution if literals are involved.

    I am using Pentaho Data Integration and dont get to specify the sql syntax. Using a very simple DB lookup gave the error "Illegal mix of collations (cp850_general_ci,COERCIBLE) and (latin1_swedish_ci,COERCIBLE) for operation '='"

    The generated code was "SELECT DATA_DATE AS latest_DATA_DATE FROM hr_cc_normalised_data_date_v WHERE PSEUDO_KEY = ?"

    Cutting the story short the lookup was to a view and when I issued

    mysql> show full columns from hr_cc_normalised_data_date_v;
    +------------+------------+-------------------+------+-----+
    | Field      | Type       | Collation         | Null | Key |
    +------------+------------+-------------------+------+-----+
    | PSEUDO_KEY | varchar(1) | cp850_general_ci  | NO   |     |
    | DATA_DATE  | varchar(8) | latin1_general_cs | YES  |     |
    +------------+------------+-------------------+------+-----+
    

    which explains where the 'cp850_general_ci' comes from.

    The view was simply created with 'SELECT 'X',......' According to the manual literals like this should inherit their character set and collation from server settings which were correctly defined as 'latin1' and 'latin1_general_cs' as this clearly did not happen I forced it in the creation of the view

    CREATE OR REPLACE VIEW hr_cc_normalised_data_date_v AS
    SELECT convert('X' using latin1) COLLATE latin1_general_cs        AS PSEUDO_KEY
        ,  DATA_DATE
    FROM HR_COSTCENTRE_NORMALISED_mV
    LIMIT 1;
    

    now it shows latin1_general_cs for both columns and the error has gone away. :)

提交回复
热议问题