为什么 MySQL 中 GROUP_CONCAT 函数返回 BLOB 大对象类型?(Why GROUP_CONCAT returns BLOB?)

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用版 2核4GB 50GB
简介: 为什么 MySQL 中 GROUP_CONCAT 函数返回 BLOB 大对象类型?(Why GROUP_CONCAT returns BLOB?)太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)本文遵循“署名-非商业用途-保持一致”创作公用协议转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作。

为什么 MySQL 中 GROUP_CONCAT 函数返回 BLOB 大对象类型?(Why GROUP_CONCAT returns BLOB?)

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es)

本文遵循“署名-非商业用途-保持一致”创作公用协议

转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载,谢谢合作。



相似的文章列于下方,

我的问题是 CONCAT(字段名, ''),这个用法,就是为了将字段名所代表的数值字段转换成字符串结果。

那么,从以下三楼回复中,我们可以摘出:CAST(user_id AS CHAR)

用 CAST 函数来将某个字段的值转换成字符型。


问题出现的原因是,CONCAT 的函数计算结果以字节形式返回,就造成被误认为是 BLOB 二进制的大对象。

所以,转换成字符串时,慎用该函数,而使用 CAST 进行转换。


使用 WorkBench 这类工具时,会有一个设置选项,可以把 二进制或可变二进制返回结果当成非二进制的字符串来看待,这样这类工具执行类似SQL语句,返回的函数计算字段值就不会被当成 BLOB 看待,而是直接显示其字符串值了。


Why GROUP_CONCAT returns BLOB?


Last week, when using the GROUP_CONCAT() function on a MySQL database, I got an unexpected result. :(

Indeed, instead of getting my result as VARCHAR types, I got it as BLOB types! For information, a BLOB is a binary large object that can hold a variable amount of data:
http://dev.mysql.com/doc/refman/5.0/en/blob.html
Because BLOB values are treated as binary strings, it is not easy to use. This is why we would prefer to haveVARCHAR values.

So the question is how to get around this frustrating problem?

The answer is, for once, very simple! :D
You simply need to:

  • Open your my.ini or my.cnf file;
  • Change the value of the group_concat_max_len system variable to 512 (no ‘k’ suffix);
  • Restart the mysql service

To verify if the value has been successfully updated, execute the following command in your mysql client:

mysql> show variables like "%concat%";
+----------------------+-------+
| Variable_name        | Value |
+----------------------+-------+
| group_concat_max_len | 512   |
+----------------------+-------+
1 row in set (0.00 sec)

Note that you cannot set the value of group_concat_max_len to less than 1Kb using the MySQL Administrator GUI. Which means that the only way to set this system variable to 512 (which is less than 1Kb) is to edit your MySQL configuration file as described above.

blob, group_concat(), group_concat_max_len, MySQL, varchar


This entry was posted on 11 Feb 2010, 19:11 and is filed under MySQL, Tricks. You can follow any responses to this entry through RSS 2.0. You can leave a response, or trackback from your own site.

  1. #1 by Gerald Mengisen on 15 Sep 2010 - 20:34

    The solution above doesn’t quite work for shared hosting. However, the solution in the link below worked for me (I’m just adding this here because your entry was one of the first ones in Google about this problem):
    http://stackoverflow.com/questions/2133936/using-group-concat-in-phpmyadmin-will-show-the-result-as-blob-3b

  2. #2 by smoreau on 15 Sep 2010 - 22:52

    Thank you very much for sharing this link.
    Indeed, it wouldn’t be possible to change the value of a system variable on a shared hosting.
    It is actually a nice trick to convert the column value to a string ! :)

  3. #3 by mart on 13 Apr 2011 - 11:25

    Had the same problem, found answer
    SELECT rec_id, GROUP_CONCAT(CAST(user_id AS CHAR))
    FROM t1
    GROUP BY rec_id

    @ http://stackoverflow.com/questions/2133936/using-group-concat-in-phpmyadmin-will-show-the-result-as-blob-3b

  4. #4 by RakonDark on 13 Jul 2012 - 17:46

    Dear mart
    I found a problem with your CAST on MySQL

    when i have INT and i Gdo this
    SELECT rec_id, GROUP_CONCAT(CAST(user_id AS CHAR))
    FROM t1
    GROUP BY rec_id

    then i have always only 1 char space for the int value , this may work for 0-9 but not when your INT grow :)

    for me it was fine to CAST as CHAR(7) to hold the
    biggest value 9999999 value from my INT:)

    SELECT rec_id, GROUP_CONCAT(CAST(user_id AS CHAR(7)))
    FROM t1
    GROUP BY rec_id









相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
存储 SQL 关系型数据库
MySQL 存储函数及调用
本文主要介绍了MySQL存储函数及调用的方法,介绍了存储函数的示例,如何在MySQL中创建存储函数,以及介绍了MySQL中如何创建触发器,一目了然。在MySQL中,存储函数是预定义的SQL逻辑单元,可接收参数并返回单个值。创建存储函数使用`CREATE FUNCTION`语句,如示例中的`square_number`函数,它接收一个整数并返回其平方。调用存储函数就像调用普通函数一样,例如在`SELECT`语句中。存储函数可用于简化复杂逻辑,提高效率。此外,还可以通过`DROP FUNCTION`删除不再需要的函数。示例展示了不同场景下的存储函数应用,包括数字运算、字符串处理和逻辑判断。
94 2
|
1月前
|
存储 SQL 关系型数据库
MySQL 进阶使用【函数、索引、视图、存储过程、存储函数、触发器】(2)
MySQL 进阶使用【函数、索引、视图、存储过程、存储函数、触发器】
|
1月前
|
存储 SQL 关系型数据库
MySQL 进阶使用【函数、索引、视图、存储过程、存储函数、触发器】(1)
MySQL 进阶使用【函数、索引、视图、存储过程、存储函数、触发器】
|
11天前
|
存储 JSON 关系型数据库
mysql中find_in_set()函数用法详解及增强函数
总结而言,`FIND_IN_SET()`是MySQL中处理由逗号分隔的字符串列表的一种便捷方法,尤其适用于列表相对较短且不经常更改的场景。然而,对于更为复杂的需要高性能和可扩展性的数据库设计,它可能不是最优选择,应考虑使用更加正规化的数据库结构。
16 2
mysql中find_in_set()函数用法详解及增强函数
|
14天前
|
关系型数据库 MySQL
mysql使用 CONCAT(字段,字段) 函数拼接
mysql使用 CONCAT(字段,字段) 函数拼接
|
28天前
|
JSON 关系型数据库 MySQL
MySQL中GROUP_CONCAT与JSON_OBJECT、GROUP BY的巧妙结合:打造高效JSON数组汇总
MySQL中GROUP_CONCAT与JSON_OBJECT、GROUP BY的巧妙结合:打造高效JSON数组汇总
18 1
|
1月前
|
JSON 关系型数据库 MySQL
MySQL常用函数解读:从基础到进阶的全方位指南
MySQL常用函数解读:从基础到进阶的全方位指南
|
1月前
|
关系型数据库 MySQL 开发者
Mysql COUNT() 函数详解
【6月更文挑战第19天】Mysql COUNT() 函数详解,包括 COUNT() 的用法及 COUNT() 带条件查询的操作
|
1月前
|
SQL 关系型数据库 MySQL
MySQL数据库基础第二篇(函数)
MySQL数据库基础第二篇(函数)
|
1月前
|
关系型数据库 MySQL
【随手记】MySQL中ROW_NUMBER()、RANK()和DENSE_RANK()函数的用法
【随手记】MySQL中ROW_NUMBER()、RANK()和DENSE_RANK()函数的用法
26 1