Bug #965 GROUP_CONCAT gibberish using string functions with joined tables
Submitted: 31 Jul 2003 7:34 Modified: 9 Aug 2003 13:28
Reporter: Jaime Almeida Email Updates:
Status: Can't repeat Impact on me:
None 
Category:MySQL Server Severity:S2 (Serious)
Version:4.1 alpha OS:Linux (Linux (Intel))
Assigned to: Vasily Kishkin CPU Architecture:Any

[31 Jul 2003 7:34] Jaime Almeida
Description:
When I use a GROUP_CONCAT function together with a string comparisson funcion in joined tables, I get gibberish in the concatenated column.
 BUT it happens only when I try selecting other fields BEFORE the CONCAT column.  If the GROUP_CONCAT is the first select statement everything is fine. 
Additionally if I use some other method instead of using the string function like comparing INT's the gibberish does not occur.

PD I reported this as a MySQLCC bug.  Its a server BUG!

How to repeat:
GIBBERISH:

select  
materias.nombre as materia,
salones.grupo, 
salones.salon,
group_concat(salones.dia_semana ORDER BY salones.ID_SALON ASC SEPARATOR ", " ) as dias, 
CONCAT(LEFT(salones.hora_ent,5),"-",LEFT(salones.hora_sal,5)) AS HORAS
from salones, datos_maestros,materias 
where 
datos_maestros.apellido_pat LIKE "%Almeida%" and
salones.id_maestro=datos_maestros.mid and
salones.id_materia=materias.id_materia
group by hora_ent

RETURNS: 

*************************** 1. row ***************************
materia: Introduccion a la Informatica
  grupo: 1
  salon: 1
   dias: 
  HORAS: 07:00-08:00
*************************** 2. row ***************************
materia: Introduccion a la Informatica
  grupo: 2
  salon: 2
   dias: 
  HORAS: 08:00-09:00
*************************** 3. row ***************************
materia: Dinamica Social de las Organizaciones
  grupo: 2
  salon: 2
   dias: 8?_þ
  HORAS: 12:00-13:00
*************************** 4. row ***************************
materia: Principios de Contabilidad
  grupo: 1
  salon: 1
   dias: 
  HORAS: 13:00-14:00
*************************** 5. row ***************************
materia: Introduccion a la Informatica
  grupo: 7
  salon: 1
   dias: 
  HORAS: 14:00-15:00
*************************** 6. row ***************************
materia: Introduccion a la Informatica
  grupo: 7
  salon: 1
   dias: Di?_0æþ
  HORAS: 16:00-17:00

No GIBBERISH:

select
group_concat(salones.dia_semana ORDER BY salones.ID_SALON ASC SEPARATOR ", " ) as dias,
CONCAT(LEFT(salones.hora_ent,5),"-",LEFT(salones.hora_sal,5)) AS HORAS,
materias.nombre as materia,
salones.grupo, 
salones.salon
from salones, datos_maestros,materias 
where 
datos_maestros.apellido_pat LIKE "%Almeida%" and
salones.id_maestro=datos_maestros.mid and
salones.id_materia=materias.id_materia
group by hora_ent

returns:

*************************** 1. row ***************************
   dias: Lu
  HORAS: 07:00-08:00
materia: Introduccion a la Informatica
  grupo: 1
  salon: 1
*************************** 2. row ***************************
   dias: Lu
  HORAS: 08:00-09:00
materia: Introduccion a la Informatica
  grupo: 2
  salon: 2
*************************** 3. row ***************************
   dias: Lu, Lu
  HORAS: 12:00-13:00
materia: Dinamica Social de las Organizaciones
  grupo: 2
  salon: 2
*************************** 4. row ***************************
   dias: Lu
  HORAS: 13:00-14:00
materia: Principios de Contabilidad
  grupo: 1
  salon: 1
*************************** 5. row ***************************
   dias: Lu
  HORAS: 14:00-15:00
materia: Introduccion a la Informatica
  grupo: 7
  salon: 1
*************************** 6. row ***************************
   dias: Mi, Lu, Ma, Ju
  HORAS: 16:00-17:00
materia: Introduccion a la Informatica
  grupo: 7
  salon: 1

Suggested fix:
Its probably has something to do with 'clean' (memory wise) GROUP_CONCAT function.
[31 Jul 2003 11:03] Vasily Kishkin
What are types of fields you use ? char or varchar ?
[1 Aug 2003 4:11] Allen Morris
The code below results in too different answers.
The firest select gives:
+--------------------------------+-----------+-----------+-------------------+
| GROUP_CONCAT(name)             | MIN(name) | MAX(name) | product           |
+--------------------------------+-----------+-----------+-------------------+
|  3                             | name 1    | name 3    | This is a title 1 |
|  1                             | name 1    | name 3    | This is a title 2 |
|  1                             | name 1    | name 3    | This is a title 3 |
+--------------------------------+-----------+-----------+-------------------+
And the second select gives:
+-----------+-----------+--------------------------------+-------------------+
| MIN(name) | MAX(name) | GROUP_CONCAT(name)             | product           |
+-----------+-----------+--------------------------------+-------------------+
| NULL      | NULL      |              ÿ              ÿ  | This is a title 1 |
| NULL      | NULL      |              ÿ              ÿ  | This is a title 2 |
| NULL      | NULL      |              ÿ              ÿ  | This is a title 3 |
+-----------+-----------+--------------------------------+-------------------+
The final select works a expected.

--------------------

DROP TABLE IF EXISTS product;
CREATE TABLE product (
  product_id mediumint(8) unsigned NOT NULL auto_increment,
  product varchar(255) default NULL,

  PRIMARY KEY  (product_id)
) TYPE=InnoDB CHARSET=latin1;

DROP TABLE IF EXISTS name;
CREATE TABLE name (
  name_id mediumint(8) unsigned NOT NULL auto_increment,
  product_id mediumint(8) unsigned NOT NULL,
  name varchar(255) default NULL,
  PRIMARY KEY  (name_id)
) TYPE=InnoDB CHARSET=latin1;

insert into product ( product ) values ('This is a title 1');
SET @id=LAST_INSERT_ID();
insert into name ( product_id, name ) values (@id, 'name 1');
insert into name ( product_id, name ) values (@id, 'name 2');
insert into name ( product_id, name ) values (@id, 'name 3');

insert into product ( product ) values ('This is a title 2');
SET @id=LAST_INSERT_ID();
insert into name ( product_id, name ) values (@id, 'name 1');
insert into name ( product_id, name ) values (@id, 'name 2');
insert into name ( product_id, name ) values (@id, 'name 3');

insert into product ( product ) values ('This is a title 3');
SET @id=LAST_INSERT_ID();
insert into name ( product_id, name ) values (@id, 'name 1');
insert into name ( product_id, name ) values (@id, 'name 2');
insert into name ( product_id, name ) values (@id, 'name 3');

set group_concat_max_len = 30;

SELECT GROUP_CONCAT(name),
       MIN(name),
       MAX(name),
       product
 FROM product
  JOIN name ON product.product_id = name.product_id
 GROUP BY product.product_id;

SELECT MIN(name),
       MAX(name),
       GROUP_CONCAT(name),
       product
 FROM product
  JOIN name ON product.product_id = name.product_id
 GROUP BY product.product_id;

SELECT GROUP_CONCAT(name),
       MIN(name),
       MAX(name)
 FROM name
 GROUP BY product_id;
[1 Aug 2003 11:29] Jaime Almeida
All my fields are varchar.
Ive found other cases where putting the GROUP_CONCAT as the first select statement also returns gibrberish.
[8 Aug 2003 20:26] Allen Morris
I am getting a cut warning on my failures.
however make int varchars 10 and doing 
  set group_concat_max_len=4096;
do not seem to have much affect.

Warning 1254    3 line(s) was(were) cut by group_concat()
[9 Aug 2003 13:28] Vasily Kishkin
Thank you for your bug report. This issue has been fixed in the latest
development tree for that product. You can find more information about
accessing our development trees at 
    http://www.mysql.com/doc/en/Installing_source_tree.html

4.1 from bk tree returns correct results for both queries.

Chance is that it is fixed after 4.1.0 with some of other GROUP_CONCAT fixes
[20 Sep 2003 16:46] Patrick Shoaf
I have a similar case. GROUP_CONCAT was working with one table, but not two.  GROUP_CONCAT was in the middle of the SELECT.  I placed GROUP_CONCAT first and works fine.  Here is my working select...

SELECT GROUP_CONCAT(Item_Size SEPARATOR ",") as sizes, Cat_Item_Img, Cat_Price, Product.Item_Code, Item_Img, Description, Category, Retail_Price, Short_Desc, Product.Item_Color FROM Cat_Items,Product WHERE cat_code ="BoltTech" and Cat_Items.Item_Code = Product.Item_Code and Category ="Casual Wear" group by Product.Item_Code

My tables are still extremely small & I could email or attach them for other to test.  I am currently running MySQL 4.1 on RedHat Linux ES Server 2.1