Paradoxis
Paradoxis

Reputation: 4708

mySQL multiple columns in group concat

I'm trying to select multiple columns from multiple tables into one single column as a string (since this is part of a larger query)

Basically what I'm trying to do is make sub-groups of an ID and a Name, The only problem however is that I can't find a way to split these after they already have had a separator assigned to the parent group.

My code:

SELECT 
    GROUP_CONCAT(`mytable1`.`id`, `mytable2`.`name` SEPARATOR  ', ')
FROM `mytable1`
INNER JOIN `mytable2`
    ON `mytable2`.`id` = `mytable1`.`id`

What I'm getting:

127Name, 153Name, 153Name, etc..

What I'm trying to achieve:

127 : Name, 153 : Name, 153 : Name, etc..

Is there any way this is possible?

Upvotes: 2

Views: 107

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

Use concat inside group_concat

GROUP_CONCAT(CONCAT(`mytable1`.`id`,' : ', `mytable2`.`name`) SEPARATOR  ', ')

Upvotes: 1

Related Questions