Detecting utf8 broken characters in MySQL

后端 未结 18 2099
广开言路
广开言路 2020-12-02 05:03

I\'ve got a database with a bunch of broken utf8 characters scattered across several tables. The list of characters isn\'t very extensive AFAIK (áéíúóÁÉÍÓÚÑñ)

Fixing

18条回答
  •  Happy的楠姐
    2020-12-02 05:43

    This is an extension of @Thales Ceolin's answer in order to modify every table in the db:

    select concat(
        "update ", 
        a.TABLE_NAME, 
        " set ", b.COLUMN_NAME, 
        " = CONVERT(BINARY CONVERT(", 
        b.COLUMN_NAME, 
        " USING latin1) USING utf8) where ",
        b.COLUMN_NAME, 
        " is not null;") query
    from INFORMATION_SCHEMA.TABLES a
    left join INFORMATION_SCHEMA.COLUMNS b on a.TABLE_NAME = b.TABLE_NAME
    where a.table_schema = 'db_name'
    and a.TABLE_TYPE = 'BASE TABLE'
    and b.data_type in ('text', 'varchar')
    and a.TABLE_NAME = 'table_name';
    

    This will result in:

    update table_name set idn = CONVERT(BINARY CONVERT(idn USING latin1) USING utf8) where idn is not null;
    update table_nameset name = CONVERT(BINARY CONVERT(name USING latin1) USING utf8) where name is not null;
    update table_name set primary_last_name = CONVERT(BINARY CONVERT(primary_last_name USING latin1) USING utf8) where primary_last_name is not null;
    

提交回复
热议问题