MySQL - GROUP_CONCAT with two columns from different tables

IlludiumPu36 :

I want to concatenate two columns from different tables in my current GROUP_CONCAT:

SELECT MAX(unit.unit_pk) AS unit_pk,
       GROUP_CONCAT(unit_outcome.unit_outcome SEPARATOR '|') unit_outcomes,
       MAX(program_outcome.program_outcome) program_outcome,
       GROUP_CONCAT(unit_outcome.unit_outcome_pk) unit_outcome_pks, 
       program_outcome.program_outcome_pk,
       program_outcome.program_outcome
FROM unit
INNER JOIN unit_unit_outcome_lookup
    ON unit_unit_outcome_lookup.unit_fk = unit.unit_pk
INNER JOIN unit_outcome
    ON unit_outcome.unit_outcome_pk = unit_unit_outcome_lookup.unit_outcome_fk
INNER JOIN program_outcome_unit_outcome_lookup
    ON program_outcome_unit_outcome_lookup.unit_outcome_fk = unit_outcome.unit_outcome_pk
INNER JOIN program_outcome
    ON program_outcome.program_outcome_pk = program_outcome_unit_outcome_lookup.program_outcome_fk
GROUP BY program_outcome_pk ORDER BY program_outcome.program_outcome ASC

so that

GROUP_CONCAT(unit_outcome.unit_outcome SEPARATOR '|') unit_outcomes

combines unit.unit_code and unit_outcome.unit_outcome inside the GROUP_CONCAT

I assume this can be done?

The result I'm looking for would be a string from the GROUP_CONCAT like "unit_code: unit_outcome"

db fiddle

Akina :
GROUP_CONCAT(CONCAT(unit.unit_code, '-', unit_outcome.unit_outcome) SEPARATOR '|')

You may use any other separator in CONCAT() instead of '-' which I have randomly used.

Also you may use CONCAT_WS() instead of CONCAT() - especially when more than 2 fields must be concatenated in one source record.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=346489&siteId=1