为什么 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! 
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.

blobgroup_concat()group_concat_max_lenMySQLvarchar

This entry was posted on 11 Feb 2010, 19:11 and is filed under MySQLTricks. 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

时间: 2024-07-28 23:36:11

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

MySQL中concat函数(连接字符串)

MySQL中concat函数使用方法:CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. 注意:如果所有参数均为非二进制字符串,则结果为非二进制字符串. 如果自变量中含有任一二进制字符串,则结果为一个二进制字符串.一个数字参数被转化为与之相等的二进制字符串格式:若要避免这种情况,可使用显式类型 cast, 例如:SELECT CONCAT(CAST(int_col AS CHAR), char_col) MySQL的con

MySQL中concat函数

使用方法:CONCAT(str1,str2,-) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. 注意:如果所有参数均为非二进制字符串,则结果为非二进制字符串. 如果自变量中含有任一二进制字符串,则结果为一个二进制字符串.一个数字参数被转化为与之相等的二进制字符串格式:若要避免这种情况,可使用显式类型 cast, 例如:SELECT CONCAT(CAST(int_col AS CHAR), char_col) MySQL的concat函数可以连接一个或者多

mysql中concat函数的使用相关总结

concat(str1,str2) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. mysql> select concat('11','22','33'); +------------------------+ | concat('11','22','33') | +------------------------+ | 112233 | +------------------------+ 1 row in set (0.00 sec) MySQL的co

Mysql中的函数

阅读目录 什么是函数 与存储过程的区别 mysql自带函数 自定义函数 什么是函数 mysql中的函数与存储过程类似,都是一组SQL集: 与存储过程的区别 函数可以return值,存储过程不能直接return,但是有输出参数可以输出多个返回值: 函数可以嵌入到sql语句中使用,而存储过程不能: 函数一般用于实现较简单的有针对性的功能(如求绝对值.返回当前时间等),存储过程用于实现复杂的功能(如复杂的业务逻辑功能): mysql自带函数 mysql本身已经实现了一些常见的函数,如数学函数.字符串函

Windows中openProcess函数返回ERROR_ACCESS_DENIED的解决方法

辛辛苦苦开始了创业,好不容易见到了天使投资人,如何去打动明星投资人?如何能拿到那一笔"救命"钱?看徐小平.雷军这样说. 1. 天使投资人偏爱投什么样的创业者? 雷军:你有强烈的渴望做成一件伟大的事情,并且能让投资者相信你能做得成这件事情.掏自己的钱创业是创业成功率最高的一种,因为在那一瞬间你重视了,你花的每一分钱都是自己的血汗钱和别人的血汗钱,不会轻松把别的投资人的钱打水漂. 曾李青:我们体系内投了好几家公司,发现我们投资成功的公司要么是有做大公司的成功经验,要么是名校毕业.好学校不一

使用SSH操作Oracle中BLOB大对象

package entity; /** * Bigobject entity. @author MyEclipse Persistence Tools */ public class Bigobject implements java.io.Serializable { // Fields private Integer id; private byte[] tblob; private String filename; // Constructors /** default construct

SqlServer和MySQL中存储过程out返回值处理C#代码

1.SqlServer中out处理 C#代码 #region"SqlServer中存储过程处理out返回值" //public void getdata() //{ // string str = " server=192.168.xxxx ;user id=xxx;[email protected]#;database=xxxxx_db;min pool size=4;max pool size=4;packet size=3072"; // SqlConnect

MySQL中ROW_NUMBER()函数的替换实现

SELECT t.*, @RowNum := @RowNum + 1 AS RowNum FROM t, (SELECT @RowNum := 0) AS myRows MySQL中没有ROW_NUMBER()函数,可以用以上代码替换. MySQL中ROW_NUMBER()函数的替换实现

hibernate 大对象类型的hibernate映射

基本知识: 在 Java 中, java.lang.String 可用于表示长字符串(长度超过 255), 字节数组 byte[] 可用于存放图片或文件的二进制数据. 此外, 在 JDBC API 中还提供了 java.sql.Clob 和 java.sql.Blob 类型, 它们分别和标准 SQL 中的 CLOB 和 BLOB 类型对应. CLOB 表示字符串大对象(Character Large Object), BLOB表示二进制对象(Binary Large Object) Mysql