将MySQL查询的输出转换为UTF8?

WBOY
发布: 2023-08-23 14:13:08
转载
1362人浏览过

将mysql查询的输出转换为utf8?

You need to use CAST() or CONVERT() function to convert output of MySQL query to UTF8. Here, I am using MySQL version 8.0.12. Let us first check the version:

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.12    |
+-----------+
1 row in set (0.00 sec)
登录后复制

In this if you use utf8 then you will get warning of aliases because it has utf8mb4. Therefore, you can avoid the warning by placing utf8mb4.

Note: Never use UTF8. For current versions, use UTF8MB4

Here is the syntax to convert output of MySQL query to UTF8:

SELECT yourColumnName1,convert(yourColumnName2 USING utf8) as anyVariableName FROM yourTableName;
登录后复制

You can use another syntax which is as follows:

SELECT yourColumnName1,CONVERT(CAST(yourColumnName2 as BINARY) USING utf8) as anyVariableName FROM yourTableName;
登录后复制

To understand the above syntax, let us create a table. The query to create a table is as follows:

mysql> create table ConvertOutputtoUtf8Demo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT,
   -> Name varchar(20),
   -> Age int,
   -> PRIMARY KEY(Id)
   -> );
Query OK, 0 rows affected (0.79 sec)
登录后复制

Insert some records in the table using insert command. The query is as follows:

mysql> insert into ConvertOutputtoUtf8Demo(Name,Age) values('John',24);
Query OK, 1 row affected (0.78 sec)
mysql> insert into ConvertOutputtoUtf8Demo(Name,Age) values('Larry',21);
Query OK, 1 row affected (0.15 sec)
mysql> insert into ConvertOutputtoUtf8Demo(Name,Age) values('Carol',26);
Query OK, 1 row affected (0.12 sec)
mysql> insert into ConvertOutputtoUtf8Demo(Name,Age) values('Mike',27);
Query OK, 1 row affected (0.18 sec)
mysql> insert into ConvertOutputtoUtf8Demo(Name,Age) values('Sam',22);
Query OK, 1 row affected (0.15 sec)
登录后复制

Display all records from the table using select statement. The query is as follows:

mysql> select *from ConvertOutputtoUtf8Demo;
登录后复制

The following is the output:

+----+-------+------+
| Id | Name  | Age  |
+----+-------+------+
|  1 | John  | 24   |
|  2 | Larry | 21   |
|  3 | Carol | 26   |
|  4 | Mike  | 27   |
|  5 | Sam   | 22   |
+----+-------+------+
5 rows in set (0.00 sec)
登录后复制

Here is the query to convert output of MySQL query to UTF8:

mysql> select Id,convert(Name using utf8) as ConvertToUtf8 from ConvertOutputtoUtf8Demo;
登录后复制

The following is the output:

+----+---------------+
| Id | ConvertToUtf8 |
+----+---------------+
| 1 | John           |
| 2 | Larry          |
| 3 | Carol          |
| 4 | Mike           |
| 5 | Sam            |
+----+---------------+
5 rows in set, 1 warning (0.00 sec)
登录后复制
登录后复制

You can use another query which is as follows:

mysql> SELECT Id,CONVERT(CAST(Name as BINARY) USING utf8) as ConvertToUtf8 FROM ConvertOutputtoUtf8Demo;
登录后复制

The following is the output:

+----+---------------+
| Id | ConvertToUtf8 |
+----+---------------+
| 1 | John           |
| 2 | Larry          |
| 3 | Carol          |
| 4 | Mike           |
| 5 | Sam            |
+----+---------------+
5 rows in set, 1 warning (0.00 sec)
登录后复制
登录后复制

以上就是将MySQL查询的输出转换为UTF8?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号