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

前端 未结 8 1400
一向
一向 2020-11-28 21:36

Error message on MySql:

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

I have gone

8条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 22:08

    The default collation for stored procedure parameters is utf8_general_ci and you can't mix collations, so you have four options:

    Option 1: add COLLATE to your input variable:

    SET @rUsername = ‘aname’ COLLATE utf8_unicode_ci; -- COLLATE added
    CALL updateProductUsers(@rUsername, @rProductID, @rPerm);
    

    Option 2: add COLLATE to the WHERE clause:

    CREATE PROCEDURE updateProductUsers(
        IN rUsername VARCHAR(24),
        IN rProductID INT UNSIGNED,
        IN rPerm VARCHAR(16))
    BEGIN
        UPDATE productUsers
            INNER JOIN users
            ON productUsers.userID = users.userID
            SET productUsers.permission = rPerm
            WHERE users.username = rUsername COLLATE utf8_unicode_ci -- COLLATE added
            AND productUsers.productID = rProductID;
    END
    

    Option 3: add it to the IN parameter definition:

    CREATE PROCEDURE updateProductUsers(
        IN rUsername VARCHAR(24) COLLATE utf8_unicode_ci, -- COLLATE added
        IN rProductID INT UNSIGNED,
        IN rPerm VARCHAR(16))
    BEGIN
        UPDATE productUsers
            INNER JOIN users
            ON productUsers.userID = users.userID
            SET productUsers.permission = rPerm
            WHERE users.username = rUsername
            AND productUsers.productID = rProductID;
    END
    

    Option 4: alter the field itself:

    ALTER TABLE users CHARACTER SET utf8 COLLATE utf8_general_ci;
    

    Unless you need to sort data in Unicode order, I would suggest altering all your tables to use utf8_general_ci collation, as it requires no code changes, and will speed sorts up slightly.

    UPDATE: utf8mb4/utf8mb4_unicode_ci is now the preferred character set/collation method. utf8_general_ci is advised against, as the performance improvement is negligible. See https://stackoverflow.com/a/766996/1432614

提交回复
热议问题