FAQs Categories
Client Server Commands
Database Structure
Table Types or Storage Engines
SQL Statements
Table Joins
Funtions and Operators
Tricky Select Queries
Speed Up Queries
Data Back Up
General Questions
1PLs Company - #1Payday.Loans Agency - Loans online and near me $100-$2500 (Same Day)
Powered by MySQL
Home / Funtions and Operators / Question No: 33

How to get comma separated all values in a single row against each ID?

I have a table using 4.1x that has data like this:
p_id email            participant_answer
2 a
2 b
2 c
2 d
2 e
1 a
1 c
1 d
1 b
1 d
5 a
5 c
5 d
5 b
5 d

What I really need is this format
p_id participants_answers
1 a, b, c, d, e
2 a, b, c, d, e
5 a, c, d, b, d

Is there any way to get the values into grouping separated by comma.

Answer No: 33

You can get all values in a single row in MySQL using its GROUP_CONCAT function. Try as follow:
SELECT p_id,GROUP_CONCAT(participant_answer ORDER BY p_id SEPARATOR ',') 
AS participant_answers
FROM table_name
WHERE email = '' GROUP BY p_id ORDER BY p_id;
For more help, you may visit

Related MySQL FAQs to the Above FAQ

What-is-MySQL-date-format-and-its-specifiers What is MySQL date format and its specifiers?

How-to-concat-values-in-MySQL How to concat values in MySQL?

How-to-convert-time-zone-in-MySQL How to convert time zone in MySQL?

What-are-comparison-functions-in-MySQL What are comparison functions in MySQL

How-to-set-conditions-on-query-data-with-CASE-construct How to set conditions on query data with CASE construct?

How-to-set-control-conditions-on-query-data-with-IF-funtion How to set control conditions on query data with IF funtion?

What-are-mathematical-functions-in-MySQL What are mathematical functions in MySQL?

How-are-aggregate-functions-in-MySQL How are aggregate functions in MySQL?

What-are-string-functions-in-MySQL What are string functions in MySQL?

What-are-temporal-or-date-time-functions-in-MySQL What are temporal or date time functions in MySQL?

What-are-NULL-value-related-functions-in-MySQL What are NULL value related functions in MySQL?

How-does-union-work-in-MySQL How does union work in MySQL?

About FAQs: Recently Added FAQs

About MySQL FAQs: Site Map | Bookmark Us | Recommend this Site to Your Friend | Contact Us

Useful Links: | | | |

© 2023
All rights reserved.