I have some trouble of figuring out how to use group_concat to produce certain output.
Here is what I have: 2 tables:
1st contains clients' information,
2nd contains animals' information for each client.
I have to produce this output:(i omitted the boundaries, but basically this is one column of the table)
Client: 25 Harris, Eddie
Animals: Koshka(cat),Gutsy(snake),Edger(snake),Fritz(cat),George(turtle),Big Mike(turtle)
This is what I've tried :
select group_concat ( distinct
'Client: ', CL.cl_id, space(1),
CL.cl_name_last,', ',
CL.cl_name_first,'\n',
'Animals: ', AN.an_name, '(',
ifnull(AN.an_type, 'No Animals'), ')', '\n') as 'Client Info'
from p_vets.vt_clients CL
join p_vets.vt_animals AN using (cl_id)
group by CL.cl_id;
I get the following output (small part from the table):
Client: 25 Harris, Eddie
Animals: Koshka(cat)
,Client: 25 Harris, Eddie
Animals: Gutsy(snake)
,Client: 25 Harris, Eddie
Animals: Edger(snake)
,Client: 25 Harris, Eddie
Animals: Fritz(cat)
,Client: 25 Harris, Eddie
Animals: George(turtle)
,Client: 25 Harris, Eddie
Animals: Big Mike(turtle)
I don't understand how to group the animals to be displayed under each client they belong to.
Thank you for your help.
The group_concat should only include the animals part.
Try this:
select concat(
'Client: ', CL.cl_id, space(1), CL.cl_name_last,', ', CL.cl_name_first,'\n',
'Animals: ', group_concat(
concat( AN.an_name,'(',coalesce(AN.an_type, 'No Animals'),')')
)
)
from p_vets.vt_clients CL
join p_vets.vt_animals AN using (cl_id)
group by cl_id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.