drop database if exists bug32243; create database bug32243 character set utf8; use bug32243; create table t1( id varchar(15) not Null default '', num integer unsigned default NULL, primary key(id) ); create unique index i_t1_num on t1(num); create table t3( id varchar(15) not NULL default '', code varchar(15) default NULL, primary key(id) ); create table t2( id varchar(15) not NULL default '', t1_id varchar(15) default NULL, t3_id varchar(15) default NULL, primary key(id) ); create index i_t2_t1_id on t2(t1_id); create index i_t2_t3_id on t2(t3_id); begin; insert into t3 values('Nfpw1vPi80iSLdt','01'); insert into t3 values('0ArMt0JhaTViT8s','abc'); insert into t3 values('Yof6un0KteP2NDE','A02Bd03'); insert into t1 values('ioas2WKzefKLPuL',20404); insert into t1 values('g03RFu01i4xYE2h',3); insert into t1 values('0YHpCnE8WnWUhnr',76423); insert into t2 values('FPgsUbFw371rPTQ','g03RFu01i4xYE2h','Nfpw1vPi80iSLdt'); insert into t2 values('p9nyxDgPjrM9L9D','g03RFu01i4xYE2h','0ArMt0JhaTViT8s'); insert into t2 values('JmMIgHiRbrAD7ux','0YHpCnE8WnWUhnr','Nfpw1vPi80iSLdt'); insert into t2 values('KBRBLGF47e2ojQG','0YHpCnE8WnWUhnr','Yof6un0KteP2NDE'); insert into t2 values('3AfqcRuBptgu12h','ioas2WKzefKLPuL','Nfpw1vPi80iSLdt'); insert into t2 values('EAQj518PICX2emR','g03RFu01i4xYE2h','Yof6un0KteP2NDE'); insert into t2 values('Q99EGmmG6yfxur8','ioas2WKzefKLPuL','Yof6un0KteP2NDE'); insert into t2 values('Q2DUqSN1t3pTBoB','ioas2WKzefKLPuL','0ArMt0JhaTViT8s'); insert into t2 values('dn2s54U7KfaIcRr','0YHpCnE8WnWUhnr','0ArMt0JhaTViT8s'); commit; select t1.num, concat("|",group_concat(t3.code SEPARATOR "|"),"|") as c from t1, t2, t3 where t1.id=t2.t1_id and t3_id=t2.t3_id group by t1.num;