MySQL FAQs
FAQs Categories
Client Server Commands
Database Structure
Table Types or Storage Engines
Indexes
SQL Statements
Table Joins
Funtions and Operators
Tricky Select Queries
Speed Up Queries
Data Back Up
General Questions
Errors
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 test@test.com a
2 test@test.com b
2 test@test.com c
2 test@test.com d
2 test@test.com e
1 test@test.com a
1 test@test.com c
1 test@test.com d
1 test@test.com b
1 test@test.com d
5 test@test.com a
5 test@test.com c
5 test@test.com d
5 test@test.com b
5 test@test.com 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 = 'test@test.com' GROUP BY p_id ORDER BY p_id;
For more help, you may visit http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

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: Wikipedia.org | Oracle.com | w3schools.com | www.php.net | Github.com

© 2024  www.mysqlfaqs.net
All rights reserved.