'How to get distinct values from a column with all its corresponding values in another column

I have a table

table_categories (id INT(11), cname VARCHAR(25),survey_id INT(11))

I want to retrieve the values for the column cname without duplication, that is distinct values but with all the values in the other column.

id  cname     survey_id
--  --------  ---------
 1  Trader     2
 2  Beginner   2
25  Human      1
26  Human      2

From the above example I want to retrieve distinct cnames with all the values of the survey_id. I don't want to use any programming language. Is there any way by using a single query. Please give me a solution in MySQL.



Solution 1:[1]

You could use group_concat

SELECT cname, GROUP_CONCAT(survey_id) as survey_ids
FROM categories 
GROUP BY cname

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frédéric Clausset