W
W
w_b_x2016-11-03 14:15:59
MySQL
w_b_x, 2016-11-03 14:15:59

How to group MYSQL output separated by comma by column?

Good day!
There is the following request:

SELECT 
    `a`.`id` AS  `projectId`,
    `a`.`url` AS `projectUrl`,
    `b`.`user` AS `access_users`
FROM `sites` AS `a`
LEFT JOIN `personal` AS `b` ON `b`.`project` = `a`.`id`

The result is as follows:
51a406030f0a45bfacd8608ca9a0aa71.jpg
You need to group the data by projectID, so that in the access_users column, there are users who have access separated by commas.
Tried to do like this:
SELECT 
    `a`.`id` AS  `projectId`,
    `a`.`url` AS `projectUrl`,
    GROUP_CONCAT(`b`.`user` SEPARATOR ',') AS `access_users`
FROM `l2_sites` AS `a`
LEFT JOIN `l2_personal` AS `b` ON `b`.`project` = `a`.`id`

But it didn’t work out, all the projects were grouped and moved into one line:
e497738468dd44d2936e5e32771f3190.jpg
Help me fix it, please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
w_b_x, 2016-11-03
@w_b_x

Solved the issue by simply adding GROUP BY `a`.`id`.
sorry for the question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question