Detecting utf8 broken characters in MySQL

后端 未结 18 2106
广开言路
广开言路 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条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-02 05:37

    You might have rows with properly encoded UTF8 and with wrongly encoded characters. In this case "CONVERT(BINARY CONVERT(post_title USING latin1) USING utf8)" will trim some fields.

    I ended up doing it this way

    update `table` set `name` = replace(`name` ,CONVERT(BINARY "ä" USING latin1),'ä');
    update `table` set `name` = replace(`name` ,CONVERT(BINARY "ö" USING latin1),'ö');
    update `table` set `name` = replace(`name` ,CONVERT(BINARY "ü" USING latin1),'ü');
    update `table` set `name` = replace(`name` ,CONVERT(BINARY "Ä" USING latin1),'Ä');
    update `table` set `name` = replace(`name` ,CONVERT(BINARY "Ö" USING latin1),'Ö');
    update `table` set `name` = replace(`name` ,CONVERT(BINARY "Ü" USING latin1),'Ü');
    update `table` set `name` = replace(`name` ,CONVERT(BINARY "ß" USING latin1),'ß');
    

提交回复
热议问题