
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
In MySQL, is there a way to turn column records into a list?
Yes, we can turn a column records into a list using the MySQL GROUP_CONCAT(). Let us first create a table −
mysql> create table DemoTable -> ( -> ClientId int, -> ClientName varchar(20) -> ); Query OK, 0 rows affected (0.88 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,'Chris'); Query OK, 1 row affected (0.54 sec) mysql> insert into DemoTable values(100,'Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100,'Adam'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(100,'David'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----------+------------+ | ClientId | ClientName | +----------+------------+ | 100 | Chris | | 100 | Robert | | 100 | Adam | | 100 | David | +----------+------------+ 4 rows in set (0.00 sec)
Here is the query to turn column records into a list −
mysql> select ClientId,group_concat(ClientName separator ',') from DemoTable group by ClientId;
This will produce the following output −
+----------+----------------------------------------+ | ClientId | group_concat(ClientName separator ',') | +----------+----------------------------------------+ | 100 | Chris,Robert,Adam,David | +----------+----------------------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Is there any easy way to add multiple records in a single MySQL query?
- Is there a way to make a list from a MySQL table in Java?
- Is there a way to list collections in MongoDB?
- Is there a way to convert Integer field into Varchar without losing data in MySQL?
- Is There a Way to ‘uniq’ by Column on Linux?
- Is there a way to limit the number of records in a certain MongoDB collection?
- Is there a way to list all the reserved words in MySQL using the MySQL command-line utility?
- Is there a way to know your current username in MySQL?
- Is there a way to select a value which matches partially in MySQL?
- Is there an easy way to rename a table in a MySQL procedure?
- Is there a way to create a MySQL “alias” while creating a VIEW?
- Is there a way to name columns in an INSERT statement in MySQL?
- MySQL query to remove Null Records in a column?
- Is there a way to subtract number of days from a date in MySQL?
- Is there a way in MySQL to reverse a boolean field with a single query?

Advertisements