Why does the group_concat of primary keys (integers) generate a BLOB as a result?

Asked

Viewed 178 times

6

Trying to answer this issue (as the AP even presented, without resorting to normalization) I tried to use the group_concat it returns me the object of type BLOB.

Table:

CREATE TABLE `teste` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nome` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

Data in the Table:

inserir a descrição da imagem aqui

Consultation:

SELECT group_concat(t.id) FROM teste t;

Upshot:

inserir a descrição da imagem aqui

Is there a problem in Mysql Query Browser? In query?

Note:

In the matter at hand, I saw examples that worked right over here nothing else. In the Workbench worked well, the same query.

  • Very strange... Could [Dit] your question including your tables, data and queries?

  • Sorry there @gmsantos, missed the query, the data are already there right?

  • A create table would be more useful than the image, if you do not mind editing.

  • In the good @gmsantos...

  • It’s there @gmsantos, I put the script to create the table.

  • Seems to be a bug in an old version of Workbench that, could post what returns the query : show variables like "%concat%"; ?

  • Um... the result is 1024.

  • I think it’s logical. I think you make a mistake between the guy inside the BDD and the guy in the results. For example, if you have a table with varchar(150) on one side and varchar(300) on the other side, you will not be able to have a result that will agree with the 2 types because you can have a result of 450 car. If now you have a varchar(300) and you want.

  • It seems that it is a Bug @Peter. As for the explanation of the logic in this I have not quite understood. I will reread your comment.

  • What I mean is that the result has no "type". What has "type" is only the data inside the BDD.

  • So it’s not a bug, this is correct @Peter?

  • Correct. As in Antony’s answer, the result cannot be of the same type as the data because in many cases, the size of the result ultrapasa the size of the data type.

Show 8 more comments

1 answer

7

According to the sql manual: group_concat

The Return value is a nonbinary or Binary string, Depending on whether the Arguments are nonbinary or Binary strings. The result type is TEXT or BLOB unless group_concat_max_len is Less than or Equal to 512, in which case the result type is VARCHAR or VARBINARY.

In summary there are two factors that determine the type of return of the aggregate function group_concat:

  1. If the variable group_concat_max_len is less than or equal to 512 variable size types will be used (VARCHAR orVARBINARY). If larger than 512 lobs will be used (TEXT or BLOB).
  2. The source type format: If it is a binary string, a binary string will be generated (VARBINARY,or BLOB), if it is textual non-binary types will be used (VARCHAR or TEXT).

In your specific case your variable group_concat_max_len is set to a value greater than 512 and the id is the type int. In summer 5.1 this number is interpreted (converted implicitly) as a binary string, whereas in recent versions it is interpreted as a non-binary string.

So the function returns:

  • a guy BLOB for version 5.1, or
  • a guy TEXT for modern versions (unless you convert the type to a binary string SELECT group_concat(BINARY t.id) AS coluna FROM teste t;)

Examples:

  • But why this only occurs in mysql 5.1 and query browser ?

  • 1

    There was a change in behavior when interpreting the int between version 5.1 and 5.5 (see update). Query browser has nothing to do with history.

  • Good @Anthony. Your answer makes more sense than mine.

  • @Anthonyaccioly then why using the Workbench 5.2.31 CE it works right, did not get this part right.

  • @Cold maybe Workbench is pointing to another instance, which returns if it rotates SHOW VARIABLES LIKE "%version%" in the Workbench?

  • @gmsantos does not seem to me: 5.1.53

  • Dude, the Query Browser was discontinued (O Mysql Workbench, among other things, took the role of UI for consultation). That being said, according to my answer, if you are using Mysql 5.1 it will interpret the value as a binary string and generate a BLOB (as in the example, which does not use Query Browser or Workbench). It may be that the Workbench is interpreting the results of the binary string or something, but if you use the table of my example you will find that the type is a LONGBLOB in version 5.1.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.