- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get values from all rows and display it a single row separated by comma with MySQL
For this, use GROUP_CONCAT(). Do not use GROUP BY clause, since GROUP_CONTACT() is a better and quick solution.
Let us first create a table −
mysql> create table DemoTable1371 -> ( -> Id int, -> CountryName varchar(40) -> ); Query OK, 0 rows affected (0.89 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1371 values(100,'US'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1371 values(100,'UK'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable1371 values(101,'AUS'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable1371 values(101,'Angola'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1371;
This will produce the following output −
+------+-------------+ | Id | CountryName | +------+-------------+ | 100 | US | | 100 | UK | | 101 | AUS | | 101 | Angola | +------+-------------+ 4 rows in set (0.00 sec)
Here is the query to get values from all rows and display it a single row separated by comma −
mysql> select group_concat(CountryName separator ',') from DemoTable1371;
This will produce the following output −
+-----------------------------------------+ | group_concat(CountryName separator ',') | +-----------------------------------------+ | US,UK,AUS,Angola | +-----------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Display all the column values in a single row separated by comma in MySQL?
- How to display data from a MySQL column separated by comma?
- How to fetch random rows in MySQL with comma separated values?
- MySQL query to get a single value from position of comma-separated string?
- How to count specific comma separated values in a row retrieved from MySQL database?
- Get the first 10 rows followed by the syntax to display remaining row records with a single MySQL query
- Find specific records from a column with comma separated values in MySQL
- Count values from comma-separated field in MySQL?
- Return only a single row from duplicate rows with MySQL
- Insert all the values in a table with a single MySQL query separating records by comma
- Fetch records from comma separated values using MySQL IN()?
- Count multiple rows and display the result in different columns (and a single row) with MySQL
- Display MySQL Results as comma separated list?
- Finding a specific row which has several ids separated by comma in MySQL?
- MySQL query to get string from one column and find its position in another column with comma separated values?

Advertisements