Illegal mix of collations error in MySql

后端 未结 19 2044
醉梦人生
醉梦人生 2020-11-29 01:11

Just got this answer from a previous question and it works a treat!

SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount 
FROM ratings         


        
相关标签:
19条回答
  • 2020-11-29 01:51

    Here's how to check which columns are the wrong collation:

    SELECT table_schema, table_name, column_name, character_set_name, collation_name
    
    FROM information_schema.columns
    
    WHERE collation_name = 'latin1_general_ci'
    
    ORDER BY table_schema, table_name,ordinal_position; 
    

    And here's the query to fix it:

    ALTER TABLE tbl_name CONVERT TO CHARACTER SET latin1 COLLATE 'latin1_swedish_ci';
    

    Link

    0 讨论(0)
  • 2020-11-29 01:51

    If you want to avoid changing syntax to solve this problem, try this:

    Update your MySQL to version 5.5 or greater.

    This resolved the problem for me.

    0 讨论(0)
  • 2020-11-29 01:52
    SELECT  username, AVG(rating) as TheAverage, COUNT(*) as TheCount
    FROM    ratings
            WHERE month='Aug'
            AND username COLLATE latin1_general_ci IN
            (
            SELECT  username
            FROM    users
            WHERE   gender = 1
            )
    GROUP BY
            username
    HAVING
            TheCount > 4
    ORDER BY
            TheAverage DESC, TheCount DESC;
    
    0 讨论(0)
  • 2020-11-29 01:55

    You need to set 'utf8' for all parameters in each Function. It's my case:

    enter image description here

    0 讨论(0)
  • 2020-11-29 01:56

    I think you should convert to utf8

    --set utf8 for connection
    SET collation_connection = 'utf8_general_ci'
    --change CHARACTER SET of DB to utf8
    ALTER DATABASE dbName CHARACTER SET utf8 COLLATE utf8_general_ci
    --change CHARACTER SET of table to utf8
    ALTER TABLE tableName CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci
    
    0 讨论(0)
  • 2020-11-29 01:56

    Make sure your version of MySQL supports subqueries (4.1+). Next, you could try rewriting your query to something like this:

    SELECT ratings.username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount FROM ratings, users 
    WHERE ratings.month='Aug' and ratings.username = users.username
    AND users.gender = 1
    GROUP BY ratings.username
    HAVING TheCount > 4 ORDER BY TheAverage DESC, TheCount DESC
    
    0 讨论(0)
提交回复
热议问题