将 MySQL 查询的输出转换为 utf8

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16051369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:22:30  来源:igfitidea点击:

Convert output of MySQL query to utf8

mysqlsqlutf-8

提问by orezvani

I have a table in my database and I want run a query like

我的数据库中有一个表,我想运行一个查询

SELECT column1, column2 FROM my_table WHERE my_condition;

but I want the mysql to return the column2in utf8 encoding. Is it any function in mysql to do such task? What is that?

但我希望 mysql 返回column2in utf8 编码。mysql 中是否有任何功能可以执行此类任务?那是什么?

回答by josh-fuggle

You can use CAST and CONVERT to switch between different types of encodings. See: http://dev.mysql.com/doc/refman/5.0/en/charset-convert.html

您可以使用 CAST 和 CONVERT 在不同类型的编码之间切换。见:http: //dev.mysql.com/doc/refman/5.0/en/charset-convert.html

SELECT column1, CONVERT(column2 USING utf8)
FROM my_table 
WHERE my_condition;

回答by Waldeyr Mendes da Silva

SELECT CONVERT(CAST(column as BINARY) USING utf8) as column FROM table 

回答by ManuelAtWork

Addition:

添加:

When using the MySQL client library, then you should prevent a conversion back to your connection's default charset. (see mysql_set_character_set()[1])

使用 MySQL客户端库时,您应该防止转换回连接的默认字符集。(见mysql_set_character_set()[1]

In this case, use an additional cast to binary:

在这种情况下,使用额外的转换为二进制:

SELECT column1, CAST(CONVERT(column2 USING utf8) AS binary)
FROM my_table
WHERE my_condition;

Otherwise, the SELECTstatement converts to utf-8, but your client library converts it back to a (potentially different) default connection charset.

否则,该SELECT语句将转换为 utf-8,但您的客户端库将其转换回(可能不同的)默认连接字符集。