Vishnu Sureshkumar
Vishnu Sureshkumar

Reputation: 2316

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.

Upvotes: 0

Views: 104

Answers (1)

You could use group_concat

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

Upvotes: 1

Related Questions